如何选择最新的ID在同一个PHP文件的新表中创建新行?

时间:2013-03-16 05:18:00

标签: php mysql

我想通过输入名称在 RESTAURANT 表中创建一个餐馆。 RestaurantID(RID)是自动增量。 但与此同时,我想在 RATING 表中创建一个最新添加的RID行。

其他帖子中的几个问题与我的情况不同,我不知道如何解决这个问题。必须从这里寻求帮助..

此代码的测试结果为: {“成功”:0,“消息”:“无法为此餐厅创建评级表!”}

餐厅名称成功添加,但评级表不起作用..有什么想法?或者我应该反过来将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;
    }
?>

3 个答案:

答案 0 :(得分:2)

首先,使用mysql_query!它已被弃用(或即将推出),并且很容易引入SQL injection vulnerabilities into your code

相反,请使用像PDO这样的库。

从那里开始执行INSERT查询you can retrieve the last inserted ID,然后在另一个查询中使用它。

一些伪代码:

$queryObj = $dbHandler->prepare('statement');
$queryObj->execute();
$last_id = $dbHandler->lastInsertId();
$dbHandler->prepare('another statement with $last_id');

答案 1 :(得分:1)

这很容易,也很容易。

如果成功执行查询,则addRestaurant代码不应返回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;
    }
?>

希望这能解决您的问题。 谢谢

答案 2 :(得分:0)

你这里的表结构很重要。如果您正在使用两个表的ID的增量ID,则可以使用mysqli_insert_id(http://www.php.net/manual/en/mysqli.insert-id.php)从第一个查询中获取上次插入的ID。

1)添加餐厅 2)在代码中选择上面的插入ID 3)附加到您的评级查询

尝试Julian H. Lam的PDO,因为它的效果也很好。