如何获取插入行的ID和更新行?

时间:2014-10-11 22:20:36

标签: php mysql mysqli

我想在我的表中获取ID然后更新 id行是自动增量

if (isset($_POST['reply'])) {
    $reply = $_POST['reply'];
    $answers = $db->query("SELECT * FROM table_name");
    while($answers_ = $answers->fetch_object()){
        if($get_answer = $db->query("UPDATE table_name SET answer ='$reply' WHERE question_id = '$answers_->question_id' LIMIT 1")){
            echo "done";    
        }
    }
}   

我知道我的代码是错误的更新所有具有相同值的行我想只更新一行

1 个答案:

答案 0 :(得分:2)

use mysqli_insert_id() - 返回上一个queryn

中使用的自动生成的id
$get_answer = $db->query("UPDATE table_name SET answer ='$reply' WHERE question_id = '$answers_->question_id' LIMIT 1")

$last_id = mysql_insert_id();