我正在尝试使用事务更新2个不同的表。对于我的第一个查询,我收到错误“没有为预准备语句中的参数提供数据”。我的第二个查询完成得很好。我知道错误是指传递给$ stmt-> bind_param()的变量。我已经检查过,三重检查并且可以确认我传递给它的所有变量实际上都包含值。我还检查过以确保传递的值由's'或'i'正确指示。我的问题是,我有一个语法错误,会给我这个错误信息,还是有任何其他建议,我如何解决它?
function updateClient($id,$first,$last,$email,$phone,$phoneCarrier,$tz,$wdh1,$wdh2,$weh1,$weh2,$goals){
global $conguest;
global $database;
$conguest->autocommit(false);
//update the client tabel
$sql="UPDATE $database.client SET clientFirst = '?', clientLast = '?', clientEmail ='?', clientPhone =?, phoneCarrierId =?, timezoneId =?, clientHour1 ='?',clientHour2 ='?',clientHour3 ='?',clientHour4 ='?'
WHERE clientId = ?";
if($stmt = $conguest->prepare($sql)){
$stmt->bind_param('ssssiissssi', $first, $last, $email, $phone, $phoneCarrier, $tz, $wdh1, $wdh2, $weh1, $weh2, $id);
$stmt->execute();
$clientupdate = $stmt->affected_rows;
$stmt->close();
}
//update the goals table
$sql = "UPDATE $database.goal SET goalContent=?, categoryId = ?, goalDate=now() WHERE goalId = ?";
foreach ($goals as $goal) {
if ($stmt = $conguest->prepare($sql)) {
$stmt->bind_param('sii', $goal['goal'],$goal['cat'], $goal['id']);
$stmt->execute();
$i = $stmt->affected_rows;
$stmt->close();
}
if($i){
$goalupdate ++;
}
}
//test that all updates worked
echo "$clientupdate, $goalupdate";
exit;
if($clientupdate ==1 && $goalupdate == 3){
$conguest->commit();
$conguest->autocommit(true);
return 1;
}else{
$conguest->rollback();
$conguest->autocommit(true);
return 0;
}
}
答案 0 :(得分:1)
删除占位符周围的单引号。取代
$sql="UPDATE $database.client SET clientFirst = '?', clientLast = '?', clientEmail ='?', clientPhone =?, phoneCarrierId =?, timezoneId =?, clientHour1 ='?',clientHour2 ='?',clientHour3 ='?',clientHour4 ='?' WHERE clientId = ?";
与
$sql="UPDATE $database.client SET clientFirst = ?, clientLast = ?, clientEmail = ?, clientPhone = ?, phoneCarrierId = ?, timezoneId = ?, clientHour1 = ?, clientHour2 = ?,clientHour3 = ?,clientHour4 = ? WHERE clientId = ?";
此外,在“clientid”之前还有一个换行符。我不知道这是否相关。