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

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.