mysqli bind_param和受影响的行错误

时间:2012-09-06 07:42:51

标签: php mysql mysqli

我是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>";
}

请帮助,我现在完全迷失了。

3 个答案:

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

答案 1 :(得分:0)

'?'的数量与使用bind_param的变量数量不匹配。你做计数,但有某种差异。

编辑: 你还应该在bind_param中使用$ _POST ['param']。并且不要将所有那些$ _POST ['变量']抽入新的。

EDIT2: 阅读normalization

答案 2 :(得分:0)

首先 - 在引用它们之后提取你的$ _POST变量 - 你需要移动然后“$ stmt-&gt; bind_param(”在$ _POST变量下面排成行

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