How to update multiple columns in mysql using php

两盒软妹~` 提交于 2019-12-23 12:43:12

问题


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?


回答1:


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



回答2:


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




回答3:


You are mixing up query syntax between INSERT and UPDATE queries, the UPDATE syntax is ;

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

You shall use the UPDATE query as follows :

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



回答4:


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



回答5:


Update SQL query, see following method:

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."'";


来源:https://stackoverflow.com/questions/32438206/how-to-update-multiple-columns-in-mysql-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!