0

我试图在准备语句中传递我的两个变量,实际上我不能传递两个变量,我的代码有错误:

Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in /Applications/XAMPP/xamppfiles/htdocs/admin/genre.php on line 4

Fatal error: Call to a member function close() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/admin/genre.php on line 9

这是我的代码:

    <?php
include("../db_inc.php");
if($stmt=$connection->prepare("INSERT INTO genre(genre_name,genre_desc) VALUES('?','?')")){
    $stmt->bind_param('sd',$genre_name,$genre_desc);
    $genre_name =$_POST["genre_name"];
    $genre_desc =$_POST['genre_desc'];
    $stmt -> execute();
     $stmt -> close();
     $mysqli -> close();
    }
$result=mysql_query($qry) or die(mysql_error());
if("$result"){
    echo "Add Successfully";
    }
?>
4

2 回答 2

2

尝试从值中删除引号...

"INSERT INTO genre(genre_name,genre_desc) VALUES('?','?')"

应该...

"INSERT INTO genre(genre_name,genre_desc) VALUES(?, ?)"

编辑-编辑:忘记这一点......我的立场是正确的

编辑:另外...

$stmt->bind_param('sd',$genre_name,$genre_desc);
$genre_name =$_POST["genre_name"];
$genre_desc =$_POST['genre_desc'];

应该...

$genre_name =$_POST["genre_name"];
$genre_desc =$_POST['genre_desc'];
$stmt->bind_param('sd',$genre_name,$genre_desc);

您在使用后声明变量。

于 2013-11-11T21:50:17.360 回答
1
$stmt->bind_param('sd',$genre_name,$genre_desc);
$genre_name =$_POST["genre_name"];
$genre_desc =$_POST['genre_desc'];

您将 $genre_name 作为字符串,并将 $genre_desc 作为绑定参数中的双精度...您确定在 $_POST['genre_desc'] 中传递的数据是双精度而不是字符串?看起来您传递了一个描述,对我来说这将是一个字符串,如下所示:

$stmt->bind_param('ss',$genre_name,$genre_desc);

PS:bind_param 变量的定义可以放在bind_param 语句之后!它们必须在语句的“执行”命令之前声明!

于 2013-11-11T22:13:47.537 回答