简体   繁体   中英

Update table column in mysql

I have a problem to update the specific column. I don't want to specify the ID in order to update that row. I am using like flag. for example I want to change column 'request' to 1. Initially it is 0. I am using this code and it did changed but not the ID that I want. Let say I click on ID =2 but it will update the column ''request' with ID = 3. What is my problem. Help me. Stuck in 2 days. I know it is simple. I have tried so many times.

$sql = "SELECT column_name FROM table_name";
$query = mysqli_query($con,$sql) or die (mysqli_error($con));
while ($row = mysqli_fetch_array($query))
{
    $column_name = $row['column_name'];

}

$sql2 = "UPDATE table_name SET column_name = 1 WHERE  ID= '$ID'";   
$query2 = mysqli_query($con, $sql2) or die (mysqli_error($con));




    ID    Colour   column_name
    1      Red         0
    2      Yellow      1

You need all your column_name 0 to 1 then use like this

$sql = "SELECT ID, column_name FROM table_name";
$query = mysqli_query($con,$sql) or die (mysqli_error($con));
while ($row = mysqli_fetch_array($query))
{
 if($row['column_name'] == 0) {
  $sql2 = "UPDATE table_name SET column_name = 1 WHERE ID= ".$row['ID'];   
  $query2 = mysqli_query($con, $sql2) or die (mysqli_error($con));
 }
}

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