简体   繁体   中英

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:

$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

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

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

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM