PDA

View Full Version : How to overwrite table data?


iceflyin
07-24-2007, 06:06 AM
'user' is a number, and the index.

$sql="INSERT INTO userInfo (user, rssURL) VALUES ('$_POST[user]','$_POST[rssURL]')";

When I run the code a second time, it gives me:
Error: Duplicate entry '19717633' for key 1


I think I need to have something before it like... Puesedocode: if exists select from userInfo (user), drop rssURL, then insert new rssURL at index(user)

Or, is there a way to overwrite entries?

undertaker78
07-24-2007, 06:17 AM
If value already exists then you can update the record
$sql = "update userInfo set rrsURL = '$_POST[rssURL]' where user = '$_POST[user]'

iceflyin
07-24-2007, 06:29 AM
Sweet, thanks!

guelphdad
07-24-2007, 01:01 PM
or even better, do both at the same time


$sql="INSERT INTO userInfo (user, rssURL) VALUES ('$_POST[user]','$_POST[rssURL]')
ON DUPLICATE UPDATE
set rrsURL = '$_POST[rssURL]' where user = '$_POST[user]'";

iceflyin
07-24-2007, 02:13 PM
Already did that. Thanks though.

Heh... This is kinda funny. I am kinda new to PHP, and couldn't figure out the syntax to integrate undertakers code. So, I actually never used his, it just set me on the right track:


$sql="INSERT INTO userInfo (user, rssURL, imageURL, feedSource) VALUES ('$_POST[user]','$_POST[rssURL]','$_POST[imageURL]','$_POST[feedSource]') ON DUPLICATE KEY UPDATE rssURL= '$_POST[rssURL]', imageURL= '$_POST[imageURL]', feedSource ='$_POST[feedSource]'";