插入查询提供“无法获取mysqli_stmt”警告,但仍将数据发送到数据库

时间:2017-10-03 13:46:54

标签: php mysqli insert prepared-statement fetch

我有一个包含2个下拉列表的表单。两者都包含数据库表中的值 当访问者提交时,接下来会发生一些事情:

  1. select查询会将选项(id)/ POST值与数据库表中的值(id)进行比较。查询将在while循环中获取。在while循环中是if-else,它控制值是否彼此相等。当他们然后运行其他。
  2. else中有insert查询,它将值(id)保存在新的数据库表中。
  3. 我为selectinsert查询使用预准备语句 获取选择查询后,我在else($selControl->close();)中关闭它并启动第二个查询(插入)。

    • 当我使用提交运行网站时,我收到选择查询的错误“无法获取mysqli_stmt”。但它仍然可以工作并插入到DB表中。
    • 当我在$selControl->close(); $insKeuze->close();}else }之后写while(包含/ <?php // Include database configuration file include ("dbConfig.php"); $kLand = $_POST["landen"]; $kGerecht = $_POST["gerechten"]; // If submit and landKeuze is not empty and gerechtKeuze is not empty // --> control and validate the values and send to database. if (isset($_POST["submit"]) && !empty($kLand) && !empty($kGerecht)) { // If query is prepared then execute the query. // Query to select data from table landen with inner join table gerechten. if ($selControl = $mysqli->prepare("SELECT landen.land_id, gerechten.gerecht_id FROM landen INNER JOIN gerechten ON landen.land_id = gerechten.land_id WHERE landen.land_id = ? AND gerechten.gerecht_id = ?")) { $selControl->bind_param("ii", $kLand, $kGerecht); if (!$selControl->execute()) { echo "Failed to execute the query controle: " . $mysqli->error; } else { $selControl->bind_result($lLand_id, $gGerecht_id); while ($selControl->fetch()) // <-- Coudn't fetch { // If selected land (land_id) is not the same as land_id in table landen // or selected gerecht (gerecht_id) is not the same as gerecht_id in table gerechten --> send echo. if ($kLand != $lLand_id || $kGerecht != $gGerecht_id) { // Message when the combination is not correct echo "<script>alert('Deze combinatie bestaat niet. Doe een nieuwe poging!');</script>"; } // Else insert the selected values in table landGerecht. else { $selControl->close(); // If query is prepared --> bind the columns and execute the query. // Insert statement with placeholders for the values to database table landGerecht if ($insKeuze = $mysqli->prepare("INSERT INTO lab_stage_danush . landGerecht (land_id, gerecht_id) VALUES ( ?, ?)")) { // Bind land_id and gerecht_id as integers with $landKeuze and $gerechtKeuze. $insKeuze->bind_param('ii', $kLand, $kGerecht); // If statement is not executed then give an error message. if (!$insKeuze->execute()) { echo "Failed to execute the query keuze: " . $mysqli->error; } $insKeuze->close(); } // Else give an error message. else { echo "Something went wrong in the query keuze: " . $mysqli->error; } } } } } else { print_r($mysqli->error); } // After sent to database go back to keuze.php. echo "<script>location.replace('keuze.php');</script>"; } ?> 时)然后我得到错误,第一个查询必须在新的准备语句(这是逻辑)之前关闭。
    • 如果没有关闭声明,也会给出“准备前准备”错误。

    我更新了我的代码 我添加了bind_param。它将数据发送到数据库,但给出错误。

    我需要做些什么来阻止错误?

    如果有人可以帮助我,请提前致谢!

    代码:insert.php

    <?php
    while ($selControl->fetch())
    {
        echo "<script>alert('". $kLand . $kGerecht . $lLand_id . $gGerecht_id . "')</script>";
        // If selected land (land_id) is not the same as land_id in table landen
        // or selected gerecht (gerecht_id) is not the same as gerecht_id in table gerechten --> send echo.
        if ($kLand == $lLand_id && $kGerecht == $gGerecht_id)
        {
            $selControl->close();
    
            // If query is prepared --> bind the columns and execute the query.
            // Insert statement with placeholders for the values to database table landGerecht
            if ($insKeuze = $mysqli->prepare("INSERT INTO lab_stage_danush . landGerecht (land_id, gerecht_id) VALUES ( ?, ?)"))
            {
                // Bind land_id and gerecht_id as integers with $landKeuze and $gerechtKeuze.
                $insKeuze->bind_param('ii', $kLand, $kGerecht);
    
                // If statement is not executed then give an error message.
                if (!$insKeuze->execute())
                {
                    echo "Failed to execute the query keuze: " . $mysqli->error;
                }
                $insKeuze->close();
            } else // Else give an error message.
                {
                    echo "Something went wrong in the insert query connection: " . $mysqli->error;
                }
    
        } else // Else insert the selected values in table landGerecht.
            {
                // Message when the combination is not correct
                echo "<script>alert('Deze combinatie bestaat niet. Doe een nieuwe poging!');</script>";
            }
    }
    ?>
    

    这种结构给出了同样的错误:

    pip3

1 个答案:

答案 0 :(得分:0)

正如您在评论中所说,您认为您的mysql连接之间存在冲突。我已经开始创建了一个应该解决这个问题的课程。

landen.php      

 class landen{

    //define our connection variable, this is set in __construct
    private $mysqli;
    //this function is called when you create the class. $l = new landen($mysqlconn)
    public function __construct($mysqli){
        $this->mysqli = $mysqli;
    }

    //setter for kLand
    private $kLand = 0;
    public function setKland($k){
        $this->kLand = $k;
    }

    //setter for kGerecht
    private $kGerecht = 0;
    public function setKGerecht($k){
        $this->kGerecht = $k;
    }

    //run is our main function here.
    //if there was a return from select, it runs the insert.
    //will return true if select + insert BOTH pass.
    public function run(){
        if($this->select()){
            return $this->insert();
        }
        return false;
    }

    private function select(){
        $q = "SELECT landen.land_id, gerechten.gerecht_id FROM landen INNER JOIN gerechten ON landen.land_id = gerechten.land_id WHERE landen.land_id = ? AND gerechten.gerecht_id = ?";
        if($stmt = $this->mysqli->prepare($q)){
            $stmt->bind_param("ii",$this->kLand,$this->kGerecht);
            if($stmt->execute()){

                while ($stmt->fetch()){
                    /*
                    In your original code, you had a check to see if 
                    your post variable was the same as your returned query variables. 
                    This was not required as you are selecting from your database with those. 
                    They will **always** equal the post variables.

                    Line I'm disputing:  if ($kLand != $lLand_id || $kGerecht != $gGerecht_id) 
                    */  

                    return true;

                }
            }else{
                print_r("Error in select execute: {$this->mysqli->error}");
            }
        }else{
            print_r("Error in select prepare: {$this->mysqli->error}");
        }
        return false;
    }

    private function insert(){
        $q = "INSERT INTO lab_stage_danush . landGerecht (land_id, gerecht_id) VALUES ( ?, ?)";
        if($stmt = $this->mysqli->prepare($q)){
            $stmt->bind_param("ii",$this->kLand,$this->kGerecht);
            if($stmt->execute){
                return true;
            }else{
                print_r("Error in insert execute {$this->myqsli->error}");
            }
        }else{
            print_r("Error in insert prepare {$this->mysqli->error}");
        }

        return false;
    }

}

这是一个关于你如何运行这个例子的例子;
insert.php

<?php
require_once("dbConfig.php");  

if(isset($_POST["submit"]) && !empty($_POST['landen']) && !empty($_POST['gerechten'])){
    //Call the class code when we need it
    require_once("landen.php");
    //create the class when we need it.
    $landen = new landen($mysqli);

    //set our variables
    $landen->setKland($_POST['landen']);
    $landen->setKGerecht($_POST['gerechten']);

    //landen run returns True if the select + insert statment passed.
    //It will return false if they fail.
    //if they fail, the page will not change and the errors will be outputted.
    //Or it's failing because nothing has been returned by the select function.

    if($landen->run()){
        //send out header to go to Keuze page,
        //you had a javascript location function here.
        header("Location: keuze.php");
        die();
    }else{
        //by default, let's say our queries are running fine and the only reason the select failed is because
        //the database couldn't find anything.
        echo "Nothing found with {$_POST['landen']} and {$_POST['gerechten']}. Please try again!";
    }

}