簡體   English   中英

更新多行而不更改id值

[英]Update multiple rows without changing id value

我們有如下表:

在此處輸入圖片說明

一旦我們點擊“ Submit ”按鈕,我將使用下面的查詢在“ tracking id ”列下面顯示隨機數,它的工作狀況很好:

$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='1'");
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='2'");

但是當我使用下面的查詢時,它沒有更新...。

$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");

代碼:

<?php

$result = mysqli_query($con,"SELECT * FROM orders");

echo "<table border='1'>
<tr>
<th>order</th>
<th>payment</th>
<th>generate</th>
<th>tracking id</th>

</tr>";

while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['order_id'] . "</td>";
echo "<td>" . $row['payment_type'] . "</td>";
echo "<td><form method='post' action='new1.php'>
 <input type = submit>
</form> </td>";

echo "<td>" . $row['tracking_id'] . "</td>";

echo "</tr>";
}
echo "</table>";
?>

new1.php

<?php
$id = $row['id']; 
$r = mt_rand(1000,9999);

$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");

?>

我認為您沒有在表格中包含詳細信息

<?php

    $result = mysqli_query($con, "SELECT * FROM orders");

echo "<table border='1'>
<tr>
<th>order</th>
<th>payment</th>
<th>generate</th>
<th>tracking id</th>

</tr>";

while ($row = mysqli_fetch_array($result)) {
    $id = $row['id'];
    echo "<tr>";
    echo "<td>" . $row['order_id'] . "</td>";
    echo "<td>" . $row['payment_type'] . "</td>";

    echo "<td>";
    if (empty($row['tracking_id'])) {
        echo "<form method='post' action='new1.php'>";
        echo "<input type ='hidden' name='id' value='$id'>
          <input type='submit'>
          </form>";
    }
    echo "</td>";
    echo "<td>" . $row['tracking_id'] . " </td > ";

    echo "</tr>";
}
echo "</table >";
?>

而且您還需要修改new1.php

<?php
$id = $_POST['id']; 
$r = mt_rand(1000,9999);

$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM