How to update multiple columns in mysql using php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32438206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:58:37  来源:igfitidea点击:

How to update multiple columns in mysql using php

phpmysqlsql-update

提问by CJAY

Here i am trying to update update multiple column values in mysql table using php.

Here i am trying to update update multiple column values in mysql table using php.

$product_id = mysqli_real_escape_string($link, $_POST['product_id']);
$product_name = mysqli_real_escape_string($link, $_POST['product_name']);
$product_category = mysqli_real_escape_string($link, $_POST['product_category']);

$sql = "UPDATE product_list (product_name, product_category, product_price,product_description,product_size_category) VALUES ('$product_name', '$product_category', '$product_price', '$product_description', '$size_category')";
}"

i have 5 column values to be updated in table, i am using variable to save data and using that variable want to update the values in table how can i do that?

i have 5 column values to be updated in table, i am using variable to save data and using that variable want to update the values in table how can i do that?

回答by Shailesh Katarmal

 $sql = "UPDATE `product_list` SET 
       `product_name` = '$product_name', 
       `product_category` = '$product_category', 
       `product_price` = '$product_price', 
       `product_description` = '$product_description', 
       `product_size_category` = '$size_category' 
  where clause..... (if required) ";

回答by Toretto

Try like this :

Try like this :

$sql = "UPDATE product_list SET product_name='".$product_name."',product_category='".$product_category."',product_price='".$product_price."',product_description='".$product_description."',size_category='".$size_category."' WHERE product_id=".$product_id;

Reference : https://dev.mysql.com/doc/refman/5.0/en/update.html

Reference : https://dev.mysql.com/doc/refman/5.0/en/update.html

回答by KAD

You are mixing up query syntax between INSERTand UPDATEqueries, the UPDATEsyntax is ;

You are mixing up query syntax between INSERTand UPDATEqueries, the UPDATEsyntax is ;

UPDATE TABLE SET col1 = val1, col2=val2... WHERE col1 = val

You shall use the UPDATEquery as follows :

You shall use the UPDATEquery as follows :

$sql = "UPDATE product_list SET product_name = '$product_name', 
product_category = '$product_category' WHERE product_id = $product_id";

回答by Rifai

Your query must be something like this :

Your query must be something like this :

"UPDATE product_list 
set 
product_name='$product_name', 
product_category ='$product_category', 
product_price='$product_price',
product_description='$product_description',
product_size_category='$size_category'
where product_id='$product_id'
"
  1. make sure you define the variable you need like $size_category etc, cause i didn't see it.
  2. use conditions like where to update specific record
  1. make sure you define the variable you need like $size_category etc, cause i didn't see it.
  2. use conditions like where to update specific record

回答by Jakir Hossain

Update SQL query, see following method:

Update SQL query, see following method:

Update database_tablename SET column_name1 = column_value1 , column_name2 = column_value2

Update database_tablename SET column_name1 = column_value1 , column_name2 = column_value2

$sql = "UPDATE product_list SET product_name='".$product_name."',product_category='".$product_category."',product_price='".$product_price."',product_description='".$product_description."',size_category='".$size_category."'";