简体   繁体   English

更新多行而不更改id值

[英]Update multiple rows without changing id value

We have Table like below : 我们有如下表:

在此处输入图片说明

Once we click on " Submit " button , I am displaying random numbers below column " tracking id " using below query , its working fine: 一旦我们点击“ 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'");

But when i use below query, its not updating.... 但是当我使用下面的查询时,它没有更新...。

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

Code : 代码:

<?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 new1.php

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

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

?>

I think that you're not including the details in the form 我认为您没有在表格中包含详细信息

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

And you also need to modify new1.php 而且您还需要修改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