Jump to content

[SOLVED] Duplicate SQL entries


r00tk1LL

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/62692-solved-duplicate-sql-entries/
Share on other sites

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";
} 

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.