简体   繁体   English

更新mysql中的表列

[英]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. 我不想指定ID以更新该行。 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. 例如,我想将列'request'更改为1。最初是0。我使用的是此代码,但确实更改了,但没有更改我想要的ID。 Let say I click on ID =2 but it will update the column ''request' with ID = 3. What is my problem. 假设我单击ID = 2,但是它将更新ID = 3的“ request”列。我的问题是什么。 Help me. 帮我。 Stuck in 2 days. 卡在2天之内。 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 您需要所有column_name 0到1,然后像这样使用

$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));
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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