我想通过输入名称在 restaurant 表中创建一个餐厅。RestaurantID(RID)是自动递增的。但同时,我想在RATING表中创建一个具有最新添加RID的行。
其他帖子中的几个问题与我的情况不同,我不知道如何解决这个问题。不得不从这里寻求帮助…
这段代码的测试结果是:{"success":0,"message":" failed to create Rating table for this restaurant!"}
餐厅名称添加成功,但评分表不工作。什么好主意吗?或者我应该做另一种方式,将RID添加到评级表中?
这是我的代码:
$response = array();
$name = "test1";
if ($rid = addRestaurant($name)){
echo "$rid"; // now it displays the right id
if(addRating($rid) == true){
$response["success"] = 1;
$response["message"] = "Restaurant Successfully Created!";
echo json_encode($response);
}else{
$response["success"] = 0;
$response["message"] = "Fail to create Rating table for this restaurant!";
echo json_encode($response);
}
}else{
$response["success"] = 0;
$response["message"] = "Fail to create Restaurant! Please Try Again Later!";
echo json_encode($response);
}
function addRestaurant($name){
// mysql inserting a new row
$result = mysql_query("INSERT INTO restaurants (Name)VALUES('$name')");
// check if row inserted or not
if ($result) {
$rid = mysql_insert_id();
echo "$rid"; //this gives me correct RID of "84" latest one.
return $rid;
} else {
return false;
}
}
function addRating($rid){
echo "$rid"; // here also the latest ID. but why return false?
$result = mysql_query("INSERT INTO `rating`(`Clealiness`, `Service`, `Quality`, `RID`) VALUES ('0', '0', '0', $rid");
if ($result) {
return true;
} else {
return false;
}
?>
首先,不要使用mysql_query
!它已被弃用(或即将被弃用),并且很容易在代码中引入SQL注入漏洞。
请使用PDO之类的库。
从那里,一旦执行INSERT查询,就可以检索最后插入的ID,并在另一个查询中使用它。
一些伪代码:
$queryObj = $dbHandler->prepare('statement');
$queryObj->execute();
$last_id = $dbHandler->lastInsertId();
$dbHandler->prepare('another statement with $last_id');
易如反掌。
如果查询成功执行,你的addressrestaurant代码不应该返回true,而是应该返回最后插入的id。你的代码应该像下面
$response = array();
$name = "test1";
if ($rid = addRestaurant($name) == true){
if(addRating($rid) == true){
$response["success"] = 1;
$response["message"] = "Restaurant Successfully Created!";
echo json_encode($response);
}else{
$response["success"] = 0;
$response["message"] = "Fail to create Rating table for this restaurant!";
echo json_encode($response);
}
}else{
$response["success"] = 0;
$response["message"] = "Fail to create Restaurant! Please Try Again Later!";
echo json_encode($response);
}
function addRestaurant($name){
// mysql inserting a new row
$result = mysql_query("INSERT INTO restaurants (Name)VALUES('$name')");
// check if row inserted or not
if ($result) {
$rid = mysql_insert_id(); //This will return the last inserted id
return $rid;
} else {
return false;
}
}
function addRating($rid){
//$test = mysql_query("SELECT MAX(RID) FROM restaurants");
//$return = mysql_fetch_assoc($test);
//$rid = $return['MAX(RID)'];
$result = mysql_query("INSERT INTO rating(`Clealiness`, `Service`, `Quality`, `RID`) VALUES (0, 0, 0, $rid");
if ($result) {
return true;
} else {
return false;
}
?>
希望这将解决您的问题。谢谢你
You Tables的结构在这里很重要。如果您正在使用两个表的ID的增量ID,您可以使用mysqli_insert_id (http://www.php.net/manual/en/mysqli.insert-id.php)从第一次查询中获得最后插入的ID。
1)添加餐厅2)选择您的最后一个插入id,如上面的代码所示3)附加到你的评级查询
试试Julian H. Lam的PDO,因为它也很有效。