r00tk1LL Posted July 31, 2007 Share Posted July 31, 2007 Hi, I have a buddy system on my site and I have found that duplicate buddies can be added into the database. Here how I have this set up: user 1 want to add user 2 as a buddy. user 1 goes to user 2's page and clicks "add as buddy" A PM goes to user 2 and asks him if he accepts or declines if user 2 clicks accept a php script is run Problem: This script need to detect if that buddy already exists, if so do nothing or overwrite the original. Right now it just adds the entry again. I am using a very simple table: UID BID --------- 1 | 2 This would mean user id 1 has a buddy with a buddy id 2 Here is the non-functional code: $query = mysql_query("UPDATE buddies SET uid='$uid', bid='$bid' WHERE uid='$uid' && bid='$bid'"); print "Buddy Has been overwritten"; //would prefer it not do anything at all if entry exists if ($query == 0) { // No records updated, so add it $query = mysql_query("INSERT INTO buddies (uid, bid) VALUES ('$uid', '$bid')"); print "Buddy Added"; } Can someone help me out, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/62692-solved-duplicate-sql-entries/ Share on other sites More sharing options...
mdnghtblue Posted July 31, 2007 Share Posted July 31, 2007 if (!sqleval("SELECT * FROM buddies WHERE uid='$uid' AND bid='$bid';")) { // No records updated, so add it $query = mysql_query("INSERT INTO buddies (uid, bid) VALUES ('$uid', '$bid')"); print "Buddy Added"; } Hope that works. Quote Link to comment https://forums.phpfreaks.com/topic/62692-solved-duplicate-sql-entries/#findComment-312079 Share on other sites More sharing options...
Barand Posted July 31, 2007 Share Posted July 31, 2007 M manual has no sqleval() function so there's a good chance yours doesn't either. As an alternative <?php $res = mysql_query("SELECT COUNT(*) FROM buddies WHERE uid='$uid' AND bid='$bid' "); if (mysql_result($res,0) == 0) { // No records found, so add it $query = mysql_query("INSERT INTO buddies (uid, bid) VALUES ('$uid', '$bid')"); print "Buddy Added"; } Quote Link to comment https://forums.phpfreaks.com/topic/62692-solved-duplicate-sql-entries/#findComment-312110 Share on other sites More sharing options...
r00tk1LL Posted July 31, 2007 Author Share Posted July 31, 2007 That last on worked perfectly!!! Thanks guys, SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/62692-solved-duplicate-sql-entries/#findComment-312133 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.