Jump to content

How Do I Make Sure Duplicate Names Are Not Entered Into My Database


confused and dazed

Recommended Posts

Hello internet. I am trying to set up a query that will check to see if there is a row in the database that has the same name the user is trying to submit. Here is my code... how do I make this work? <?php$con = mysql_connect("xxx","xxx","xxx");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("xxx", $con);$entry=mysql_query("Select FROM testmysql where name='$_POST[rname]'");if($entry='$_POST[rname]');{echo "There is an entry that already exists with the name you selected. Please change the name and re-submit. Thanks.";die();

Link to comment
Share on other sites

$entry is only going to be a mysql_resourcehttp://php.net/manua...mysql-query.php you need to check if any matches were found. simple as checking the number of rows returned for instance.http://php.net/manua...ql-num-rows.php edit: also, you are using an assignment operator, terminating the if statement before the curly brace, and the curly braces don't match. At the very least you have error reporting turned on while you are developing.

if($entry='$_POST[rname]');{

Link to comment
Share on other sites

O.K. so here is an update but I cant get it to work<?php$con = mysql_connect("xxx","xxx","xxx");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("xxx", $con);$result=mysql_query("Select FROM testmysql where name='$_POST[name]'");$num_rows = mysql_num_rows($result);if($num_rows != 0);{echo "There is an entry that already exists with the name you selected. Please change the name on your and re-submit. Thanks.";die();}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...