karimali831 Posted April 29, 2010 Share Posted April 29, 2010 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 https://forums.phpfreaks.com/topic/200179-prevent-duplicated-entries/ Share on other sites More sharing options...
karimali831 Posted April 29, 2010 Author Share Posted April 29, 2010 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 https://forums.phpfreaks.com/topic/200179-prevent-duplicated-entries/#findComment-1050584 Share on other sites More sharing options...
Psycho Posted April 29, 2010 Share Posted April 29, 2010 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 https://forums.phpfreaks.com/topic/200179-prevent-duplicated-entries/#findComment-1050586 Share on other sites More sharing options...
karimali831 Posted April 29, 2010 Author Share Posted April 29, 2010 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 https://forums.phpfreaks.com/topic/200179-prevent-duplicated-entries/#findComment-1050594 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.