php mysqli命令现在无法执行错误

时间:2016-05-03 08:05:39

标签: php mysql mysqli

我试图在这个php代码的帮助下更新我的SQL表:

$description = "Something about myself";
$insert = $con->prepare("INSERT INTO kys_write (Author, writing , Title , Description , Assets) VALUES (?,?,?,?,?)");
$insert->bind_param("ssssi",$author,$data,$title,$description, $ImageExist);
$insert->execute();

$statement = $con->prepare("SELECT id FROM kys_write WHERE Title=?");
$statement->bind_param("s",$title);
$statement->execute();
$statement->bind_result($lastId);

//Everything works fine if this whole part is removed
$sql = "UPDATE  kys_essentials SET LastId=".$lastId;
if ($con->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $con->error;
}

我收到错误:

  

更新记录时出错:命令不同步,您现在无法运行此命令

是什么导致这种情况,我该如何避免呢?

2 个答案:

答案 0 :(得分:1)

清楚的是,在同一连接上执行另一个预准备语句之前,需要完全获取预准备语句执行的结果集。

您可以使用一个查询简化代码。不使用额外的选择查询

$sql = "UPDATE  kys_essentials SET LastId = (SELECT id FROM kys_write WHERE Title='$Title')";
if ($con->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $con->error;
}

答案 1 :(得分:0)

在绑定后获取$lastId并将' '放入值/ s,如下所示:

//your codes
$statement->bind_result($lastId);

while ($statement->fetch()){
      $sql = "UPDATE kys_essentials SET LastId='".$lastId."'";
      if ($con->query($sql) === TRUE){
        echo "Record updated successfully";
      } else{
          echo "Error updating record: " . $con->error;
      }
}