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 ^_^

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.