使用两个变量在Mysqli中准备Statement

时间:2013-11-11 21:45:48

标签: php mysql mysqli

我正在尝试在prepare语句中传递我的两个变量,实际上我无法传递两个变量,我的代码有错误:

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

2 个答案:

答案 0 :(得分: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);

您在使用变量后声明这些变量。

答案 1 :(得分: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的变量!它们必须在声明的“执行”命令之前声明!