Jump to content

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


confused and dazed
 Share

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]');{

Edited by thescientist
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...