更新由(自动增量)

时间:2017-10-12 06:07:53

标签: php mysql

有点混淆如何解决这个问题,我有一个名为post的表,列号为............ post_id,我要做的是用id更新post_id ,如果id = 1,post_id也将= 1,这是我到目前为止的代码

<?php
$usernme = ($_POST['test']);

$id = $_SESSION['elchAppXD'];

$ac = 1;

$queryNewPlege = "INSERT INTO posts (text, timeline_id, active) VALUES ('$usernme', '$id', '$ac')";

if (mysqli_query($dbhandle, $queryNewPlege)) {
    $upQuery = "UPDATE posts SET post_id = id";
}
// echo "i";
// if($_SESSION['Role'] == 4)
{

}
?>

这里是一段代码,假设要更新它,但我很困惑,因为我不知道如何获取id并将其重新插入post_id $upQuery = "UPDATE posts SET post_id = id";。请任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

该更新查询将是,

  $updateId = $mysqli->insert_id;
  $upQuery = "UPDATE posts SET post_id = $updateId where id = $updateId";

答案 1 :(得分:1)

您可以使用此mysqli_insert_id

获取最后一个插入ID
if(mysqli_query($dbhandle,$queryNewPlege)){

    $getLastInsertID = mysqli_insert_id($dbhandle)

    $upQuery = "UPDATE posts SET post_id = getLastInsertID";
}

答案 2 :(得分:0)

尝试这样:

$queryNewPlege = "INSERT INTO posts (text, timeline_id, active) VALUES ('$usernme', '$id', '$ac')";

if (mysqli_query($dbhandle, $queryNewPlege)) {
    $id = $mysqli->insert_id;
    $upQuery = "UPDATE posts SET post_id =".$id."WHERE id=".$id;
}