如何更新更新的行?

时间:2019-07-10 16:11:10

标签: php mysqli

当index.php加载时,它会回显select语句的结果,同时将所选行的状态更新为1。现在我的问题是,如何在用户离开页面后将这些行设置回状态0? / p>

index.php

    include("con.php");

    $ids = [];
    $query = $mysqli->prepare("SELECT id, name, age FROM table WHERE status=0 ORDER BY RAND() LIMIT 5");
    $query->execute();
    $query->bind_result($id, $name, $age);

    while ($query->fetch()) {
        $ids[] = $id;
        echo    "<p>$name</p>";
        echo    "<p>$age </p>";

    }
    $query->close();

    $query2 = $mysqli->prepare("UPDATE table SET status=1 WHERE id IN (".implode(", ", array_fill(0, count($ids), "?")).")");
    $query2->bind_param(str_repeat("i", count($ids)), ...$ids);
    $query2->execute();
    $query2->close();


    ?>

<script>
$( ".link" ).click(function() {
    $.ajax({
        url: 'reset.php'
    });
}); 
</script>

当用户单击链接时,我尝试使用ajax调用reset.php。 reset.php将所有状态更新为0,但我需要完全更新与上述相同的5行。

reset.php

  <?php
    include('index.php');

    $stmt = $mysqli->prepare("UPDATE table SET Status=0");
    $stmt->execute();
    $stmt->close();
   ?>

1 个答案:

答案 0 :(得分:0)

您可以使用php RAND()函数生成五个id,而不是使用mysql mt_rand函数,并将其保留在变量中,并在查询中使用它们。

这样,您就可以准确地知道用户已更新了行,然后当用户离开时,您可以更新了确切的五行

相关问题