Jump to content

insert if there is nothing to update


violinrocker

Recommended Posts

okay so im not familiar with ON DUPLICATE KEY UPDATE. i googled it and i dont know how to apply it.

i saw one that says it should be like this

INSERT INTO TABLE (column1, column2, column3) VALUES (value1, value2, value3) ON DUPLICATE KEY UPDATE SET column1=value1, column2=value2, column3=value3";

 

how can i make it to work like... insert into table "thisname" but if there is already a data with name="thisname" even though other fields do not match itll update them... or if it works vice versa... update then if there is none... insert.

 

sorry im a noob, please be patient with me ^_^

whats wrong with this?

$sql = "UPDATE `users` SET `css` = '$_POST[css]' WHERE `users`.`username` = '$session->username' LIMIT 1";
$sql2 = "INSERT INTO user_extension (username, sig) VALUES ($session->username, $_POST[sig]) ON DUPLICATE KEY UPDATE SET $username=$session->username, sig=$_POST[sig]";
if (!mysql_query($sql,$con))
  {?>Attention!<? die('Error: ' . mysql_error()); ?><?
  }
  if (!mysql_query($sql2,$con))
  {?>Attention!<? die('Error: ' . mysql_error()); ?><?
  }}
?>CSS & sig Success!<?

}
?>

 

 

 

 

ERROR

Attention!Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET =Violinrocker, sig=janime' at line 3

hmmm another error....

 

$sql2 = "INSERT INTO user_extension (username, sig) VALUES ($session->username, $_POST[sig]) ON DUPLICATE KEY UPDATE $_POST[sig]=sig";

 

 

$session->username=violinrocker

Attention! SIG Error: Unknown column 'Violinrocker' in 'field list'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.