3

我是 mysqli 的新手,我已经检查了 PHP 手册等,没有任何效果。我不太确定出了什么问题。我不断收到此错误:

Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\new1\template.php on line 165

还有这个

Warning: mysqli_stmt_affected_rows() [function.mysqli-stmt-affected-rows]: Couldn't fetch mysqli_stmt in C:\xampp\htdocs\new1\template.php on line 190

编码

$query = "INSERT INTO pets (name, age, gender, dob, breed_type, breed1, breed2, color, spay, size, hair_length, vaccine, vaccinated, dewormed, adoption_fees, aboutpet, petpic1, petpic2, petpic3) VALUES ('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?')";

// PREPARE QUERY TO BE EXECUTED.
$stmt = $mysqli->prepare($query);

$stmt->bind_param("sssssssssssssssssss",$petname, $petage, $petgender, $petdob, $petbreedtype, $petbreed1, $petbreed2, $petcolor, $petspay, $petsize, $pethair, $petvaccine, $petvaccineamount, $petdewormed, $petadoptfees, $aboutpet, $target_path, $target_path1, $target_path2 );

$petname  = $_POST['petname'];
$petage   = $_POST['petage'];
$petgender = $_POST['petgender'];
$petdob = $_POST['petdob'];
$petbreedtype = $_POST['petbreedtype'];
$petbreed1 = $_POST['petbreed1'];
$petbreed2 = $_POST['petbreed2'];
$petcolor = $_POST['petcolor'];
$petspay = $_POST['petspay'];
$petsize = $_POST['petsize'];
$pethair = $_POST['pethair'];
$petvaccine = $_POST['petvaccine'];
$petvaccineamount = $_POST['petvaccineamount'];
$petdewormed = $_POST['petdewormed'];
$petadoptfees = $_POST['petadoptfees'];
$aboutpet = $_POST['aboutpet'];

//EXECUTE QUERY
$stmt->execute();

//CLOSE EXECUTE
$stmt->close();

$rowcount = mysqli_stmt_affected_rows($stmt);
if ($rowcount > 0)
{
echo "<div class='noerror'>Form has been submitted successfully!</div>";
}

请帮忙,我现在完全迷路了。

4

3 回答 3

5

您不能引用?占位符:这样做会导致它们被解析为字符串文字而不是占位符。

所以:

$query = "
  INSERT INTO pets (
    name, age, gender, dob, breed_type, breed1, breed2, color, spay, size,
    hair_length, vaccine, vaccinated, dewormed, adoption_fees, aboutpet,
    petpic1, petpic2, petpic3
  ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
";
于 2012-09-06T07:47:16.347 回答
0

'?' 的数量与您使用 bind_param 输入的变量数量不匹配。您进行计数,但存在某种差异。

编辑:您还应该在 bind_param 中使用 $_POST['param']。并且不要将所有这些 $_POST['variables'] 注入新的。

EDIT2:阅读规范化

于 2012-09-06T07:44:46.080 回答
0

首先-在引用它们后提取 $_POST 变量-然后需要将 "$stmt->bind_param(" 行移到 $_POST 变量下方

其次 - 你不需要在你的'?'周围加上引号。变量 - 如果你这样做,它会将它们视为字符串而不是绑定参数

于 2012-09-06T07:50:02.803 回答