0

I have a file that i have inserting data into a mySQL Database i want to insert an UPDATE sql query in the script

i have something like this

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

I want to put an If/Else statement if it exist. and will update the record.

I have tried something like this but wont work

$sql = "UPDATE table SET ($cols) VALUES ($vals) WHERE col1 =" . $value1;
Mr. Radical
  • 1,847
  • 1
  • 19
  • 29
Big Al Ruby Newbie
  • 69
  • 1
  • 2
  • 14
  • Similar to: http://stackoverflow.com/questions/6107752/how-to-perform-an-upsert-so-that-i-can-use-both-new-and-old-values-in-update-par other than that search on upsert or merge. – user2672165 Mar 08 '14 at 12:47

2 Answers2

0

Update query in Mysql is

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

I guess you need to work on your update query.

pareto
  • 196
  • 5
0

change that

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

to

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

and this

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

to

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

then use this

 $sql = "INSERT INTO table ($cols) VALUES ($vals)
         ON DUPLICATE KEY UPDATE
         SET col1 = ". $value1;
echo_Me
  • 37,078
  • 5
  • 58
  • 78