更新多行而不更改id值

时间:2018-10-25 11:49:24

标签: php

我们有如下表:

enter image description here

点击“ 提交”按钮后,我将使用以下查询在“ 跟踪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'");

?>

1 个答案:

答案 0 :(得分:1)

我认为您没有在表单中包含详细信息

<?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'");

?>