插入查询给出 "Couldn't fetch mysqli_stmt" 警告但仍将数据发送到数据库

Insert query gives "Couldn't fetch mysqli_stmt" warning but sends still data to database

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

  1. select 查询将比较选项 (id's)/POST 值与数据库 tables 中的值 (id's)。查询将在 while 循环中获取。在 while 循环中是一个 if-else,它控制值是否彼此相等。当他们是 运行 其他时候。
  2. else 中有 insert 查询将值 (id) 保存在新数据库 table.

我对 selectinsert 查询都使用准备好的语句。
获取 select 查询后,我在 else ($selControl->close();) 中将其关闭并开始第二个查询(插入)。

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

我需要做什么来停止错误?

如果有人能帮助我,在此先感谢!

代码:insert.php

<?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>";
}
?>

这个结构给出同样的错误:

<?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>";
        }
}
?>

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

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!";
    }

}