【问题标题】:PHP Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, bool given in [duplicate]PHP 警告:mysqli_stmt_bind_param() 期望参数 1 为 mysqli_stmt,bool 在 [重复]
【发布时间】:2021-01-28 07:53:47
【问题描述】:

有人知道为什么我的代码会出现这个错误吗?它也出现在它下面的两条线上。我认为我的查询可能有问题,但我的查询对我来说似乎是正确的。我还认为这可能与 mysqli_stmt_bind_param 中的“s”有关,但我真的不确定,所以任何帮助都会很棒。

makeabooking.php 代码:

<!DOCTYPE HTML>
<html><head><title>Make a Booking</title> </head>
 <body>

<?php
//function to clean input but not validate type and content
function cleanInput($data) {  
 return htmlspecialchars(stripslashes(trim($data)));
}

//the data was sent using a formtherefore we use the $_POST instead of $_GET
//check if we are saving data first by checking if the submit button exists in the array
if (isset($_POST['submit']) and !empty($_POST['submit']) and ($_POST['submit'] == 'Book')) {
//if ($_SERVER["REQUEST_METHOD"] == "POST") { //alternative simpler POST test    
  include "config.php"; //load in any variables
  $DBC = mysqli_connect("127.0.0.1", DBUSER, DBPASSWORD, DBDATABASE);


  if (mysqli_connect_errno()) {
    echo "Error: Unable to connect to MySQL. ".mysqli_connect_error() ;
    exit; //stop processing the page further
  };

 //validate incoming data - only the first field is done for you in this example - rest is up to you do



//customernumber
$error = 0; //clear our error flag
$msg = 'Error: ';
if (isset($_POST['contactnumber']) and !empty($_POST['contactnumber']) and is_string($_POST['contactnumber'])) {
   $cn = cleanInput($_POST['contactnumber']); 
   $contactnumber = (strlen($cn)>50)?substr($cn,1,50):$cn; 
   //check length and clip if too big
   //we would also do context checking here for contents, etc       
} else {
   $error++; //bump the error flag
   $msg .= 'Invalid'; //append eror message
   $cn = '';  
} 

 
//save the customer data if the error flag is still clear
 if ($error == 0) {
    $query = ("INSERT INTO booking contactnumber VALUES (?)");
    $stmt = mysqli_prepare($DBC,$query); //prepare the query
    mysqli_stmt_bind_param($stmt,'s',$cn); 
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);    
    echo "<h2>Booking saved</h2>";        
} else { 
  echo "<h2>$msg</h2>".PHP_EOL;
}      
mysqli_close($DBC); //close the connection once done
}
?>

<form method = "post" action = "makeabooking.php">
<p>  
<label for="contactnumber">Contact number: </label>
<input type="text" name="contactnumber" required> 
</p>

</form>
</body>
</html>

【问题讨论】:

  • 查询准备不正确(INSERT INTO booking contactnumber VALUES (?) 可能是INSERT INTO booking (contactnumber) VALUES (?))。始终检查 mysqli_prepare() 调用的结果。

标签: php html sql


【解决方案1】:

INSERT INTO booking contactnumber

不正确,不。假设contactnumber是列名,那么

INSERT INTO booking (contactnumber) ...

应该是正确的。

文档:https://dev.mysql.com/doc/refman/8.0/en/insert.html

另请参阅本指南,了解如何获取真正的 mysqli 错误,以便更轻松地解决此类问题:https://stackoverflow.com/a/22662582/5947043

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多