Jump to content

Prevent duplicated entries


karimali831

Recommended Posts

Hello,

 

I'm trying to prevent users from signup up if they use a same name in my database.

Is there anything wrong with my queries?

 

The problem is that they can still signup even with the same name in the database.

 

        
$getclans = safe_query("SELECT name FROM ".PREFIX."cup_all_clans");
$db=mysql_fetch_array($getclans); 
$name = $db['name'];


$check = safe_query("SELECT * FROM ".PREFIX."cup_all_clans WHERE name='$name'");
$db=mysql_fetch_array($check); 
if($_POST['name']==$name) {

die('There is already a team with this name!'); 
}

 

Thanks for any help.

Link to comment
Share on other sites

Solved with trim :)

 

                
$name = htmlspecialchars(mb_substr(trim($_POST['name']), 0, 30));
$check = safe_query("SELECT * FROM ".PREFIX."cup_all_clans WHERE name='$name'");
$num = mysql_num_rows($check);
if($num) die('A team with this name already exists.');

Link to comment
Share on other sites

That code makes no sense. The first query gets ALL names from the DB, but only extractst he first one. The 2nd query serves no purpose. Then you compare the input name with the first name extracted from the first query. You only need one query

 

$name = mysql_real_escape_string($db['name']);
$query  = "SELECT name FROM ".PREFIX."cup_all_clans WHERE name='$name'";
$result = safe_query($query);

if(mysql_num_rows($result)>0)
{
    echo "There is already a team with the name '$name'!";
}
else
{
    //not a duplicate, proceed
}

Link to comment
Share on other sites

Thanks for that but this seems to work fine anyway...

 

$name = htmlspecialchars(mb_substr(trim($_POST['name']), 0, 30));
$check = safe_query("SELECT * FROM ".PREFIX."cup_all_clans WHERE name='$name'");
$num = mysql_num_rows($check);
if($num) die('A team with this name already exists.');

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.