使用数组更新SQL查询

时间:2014-03-08 12:40:35

标签: php mysql sql

我有一个文件,我已将数据插入mySQL数据库我想在脚本中插入UPDATE sql查询

我有类似的东西

$cols = "col1, col2, col3, col4)";
list($value1, $value2, $value3, $value4) = $items;
array_push($posted_content, $items);

vals = "";
vals .= "$value1 ,"
vals .= "$value2 ,"
vals .= "$value3 ,"
vals .= "$value4 ,"

$sql = "INSERT INTO table ($cols) VALUES ($vals)";

如果存在,我想放置一个If / Else语句。并将更新记录。

我尝试过类似的东西,但不会工作

$sql = "UPDATE table SET ($cols) VALUES ($vals) WHERE col1 =" . $value1;

2 个答案:

答案 0 :(得分:0)

在Mysql中更新查询

'UPDATE [tableName] SET col1='value1', col2='value2' WHERE id = requiredRecord '

我猜您需要处理更新查询。

答案 1 :(得分:0)

改变

 $cols = "col1, col2, col3, col4)";

  $cols = " col1, col2, col3, col4 ";

和这个

 vals = "";
 vals .= "$value1 ,"
 vals .= "$value2 ,"
 vals .= "$value3 ,"
 vals .= "$value4 ,"

$vals = "";
$vals .= "$value1 ,";
$vals .= "$value2 ,";
$vals .= "$value3 ," ;
$vals .= "$value4 ,";

然后使用此

 $sql = "INSERT INTO table ($cols) VALUES ($vals)
         ON DUPLICATE KEY UPDATE
         SET col1 = ". $value1;