简体   繁体   中英

mysql update is updating all rows instead of one

I am having an issue that I cant seem to figure out what is wrong. I am aware of SQL injection but considering the users I am not worried.

Ok so the issue I have is when i update. If i put $update = "UPDATE robert SET req='$reqs' WHERE id='$x'"; it does nothing and if i put $update = "UPDATE robert SET req='$reqs'"; it updates the entire column, which is not what i want. Here is the complete code:

<?php
                $i = 0;
                $x = 0;
                $sql = "SELECT * FROM robert";
                $result = mysqli_query($conn, $sql);

                while($row = mysqli_fetch_assoc($result)){
                    echo "<form action='rmcclanahan.php' method='post'>
                    <table><tr>";
                    echo "<td><input type='text' name='date".$i."' value='".$row['date']."' size='8'></td>";
                    echo "<td><input type='text' name='req".$i."' value='".$row['req']."' size='6'></td>";
                    echo "<td><input type='text' name='po".$i."' value='".$row['po']."' size='6'></td>";
                    echo "<td><input type='text' name='requestor".$i."' value='".$row['requestor']."' size='6'></td>";
                    echo "<td><input type='text' name='line".$i."' value='".$row['line']."' size='5'></td>";
                    echo "<td><input type='text' name='description".$i."' value='".$row['description']."' size='12'></td>";
                    echo "<td><input type='text' name='price".$i."' value='".$row['price']."' size='12'></td>";
                    echo "<td><input type='text' name='supplier".$i."' value='".$row['supplier']."' size='6'></td>";
                    echo "<td><input type='text' name='eleven".$i."' value='".$row['eleven']."' size='12'></td>";
                    echo "<td><input type='text' name='carcap".$i."' value='".$row['carcap']."' size='12'></td>";
                    echo "<td><input type='text' name='carnum".$i."' value='".$row['carnum']."' size='6'></td>";
                    echo "<td><input type='text' name='engineering".$i."' value='".$row['engineering']."' size='12'></td>";
                    echo "<td><input type='text' name='costcenter".$i."' value='".$row['costcenter']."' size='6'></td>";
                    echo "<td><input type='text' name='accnum".$i."' value='".$row['accnum']."' size='5'></td>";
                    echo "<td><input type='text' name='recieved".$i."' value='".$row['recieved']."' size='6'></td>";
                    echo "<td><input type='text' name='recdate".$i."' value='".$row['recdate']."' size='8'></td>";
                    echo "<td><button type='submit' name='submit".$i."'>Edit</button></td>";
                    echo "</tr></table>
                    </form>";


                    $i++;
                    $x++;

                    }
                    if(isset($_POST['submit'.$i.''])){
                            $reqs = $_POST['req'.$i.''];
                            $update = "UPDATE robert SET req='$reqs' WHERE id='$x'";
                            $qry = mysqli_query($conn, $update);

                            }

The query should be like this:

$update = "UPDATE robert SET req='$reqs' WHERE id='{$row['id']}'";

$X is a number that increment in every iteration, and id can be different to $x

In your code,

$update = "UPDATE robert SET req='$reqs' WHERE id='$x'";

Instead of this line just change it to this->

$update = "UPDATE robert SET req= " . $reqs . " WHERE id=" . $x;

You have got the problem because sometimes it cannot get the variable values and to solve this just concat the variables and use it. I don't check it, But hope it will work.

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