0

我目前正在创建一个在线预订系统。

如何一次执行多个查询?

我已经在使用 mysqli_muti_query。我要执行三个查询。

<?php
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";

$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ";

$sql .= "UPDATE vehicles set status = '$un'  WHERE plateno = '$plate' "; ?>
4

3 回答 3

0

您需要在每个语句后添加分号:

<?php
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";

$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ;";

$sql .= "UPDATE vehicles set status = '$un'  WHERE plateno = '$plate' ;"; 
于 2015-10-19T05:25:41.147 回答
0

如果您的数据库支持,我建议您单独运行查询并将它们包装到单个事务中。

$conn->begin_transaction();
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";
$conn->query($sql);

$sql = "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye';";
$conn->query($sql);

$sql = "UPDATE vehicles set status = '$un'  WHERE plateno = '$plate';";
$conn->query($sql);

$conn->commit();

我还建议您使用带参数的准备好的语句,而不是将值直接包含在查询字符串中。

于 2015-10-19T05:27:08.267 回答
0

您可以使用 PHPmysqli_multi_query-函数。

$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";

$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ;";

$sql .= "UPDATE vehicles set status = '$un'  WHERE plateno = '$plate' ;"; 

/* execute multi query */
if ($mysqli->multi_query($query)) {

}
于 2015-10-19T06:14:02.947 回答