Jump to content


stop duplicate enteries into a database and check to see if user exists


  • Please log in to reply
6 replies to this topic

#1 steeko1990



  • Members
  • Pip
  • 3 posts

Posted 05 May 2013 - 09:14 PM

hi guys I am i am sorry to be a bother but i am new to php and I am currently in the process of creating, login and register pages that store a users name and password in to a database but I am finding it hard to source any code to check if the name exists and if it does tell the user that it does and that they have to choose a alternative user name. The code I have for updating the database is:





extract ($_POST);
$con = mysql_connect("localhost","root","");
if (!$con)
  die('Could not connect: ' . mysql_error());
mysql_select_db("firstone", $con);
$username = $_POST['username'];
$password = $_POST['password'];
// just to show the collection and storage of information from the form
mysql_query("INSERT INTO details (username , password) VALUES ('$username', '$password')");
echo "your account has been created click next to continue";

#2 Phear46


    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 05 May 2013 - 09:25 PM

do a query to 'select' all the rows matching your input before you add it to the table. then:


if rows returned > 0 {

user exists

} else {

process form as you already are



a query like:

SELECT * FROM table WHERE user=$usrname, pass=$pass

is what your looking for, although i highly doubt ive got the correct syntax there as im also a noob, one of the pros should be able to help with that!

#3 davidannis


    Freak Dr.

  • Members
  • PipPipPip
  • 620 posts
  • LocationOkemos, MI USA

Posted 05 May 2013 - 09:26 PM

Phear46 has a reasonable suggestion. Don't select on the password, just the username.


A few more suggestions:

Use mysqli instead of mysql

when creating your table make the field username a PRIMARY or UNIQUE index.

Salt and hash passwords.

Edited by davidannis, 05 May 2013 - 09:27 PM.

#4 steeko1990



  • Members
  • Pip
  • 3 posts

Posted 05 May 2013 - 09:42 PM

sorry to be a bother i know i am really out of my depth but could anyone direct me to anywhere that i could find the corrent syntax for if rows >0.

#5 blacknight


    Advanced Member

  • Members
  • PipPipPip
  • 266 posts
  • Locationsaint john

Posted 05 May 2013 - 10:00 PM



php.net your friend when lurning allways good code examples there

if at first your fail google will give you a pass

#6 RonnieCosta50



  • Members
  • PipPip
  • 29 posts

Posted 06 May 2013 - 08:18 AM

include 'dbConnect.php'; // Database Connection php file.
include 'dbTableName.php'; // Contains 1 line of code.  The name of your table in the database.  I call it $dbTableName.
include 'dbTableColumns.php'; // The names of all your columns in your database.  Like your primary key is $dbColumn1.

$qryDuplicateString = mysql_query("SELECT COUNT(*) AS count FROM $dbTableName WHERE $dbColumn2 = '$wsTextbox1' AND $dbColumn3 = '$wsDropDownList1'");
$duplicateRows = mysql_fetch_assoc($qryDuplicateString);
$numOfRows = $duplicateRows['count'];
if ($numOfRows<1) // If a row doesn't exist,
{echo"Create Account";}
{echo"Choose alternative username";}

Hope this helps. 

Edited by RonnieCosta50, 06 May 2013 - 08:19 AM.

#7 steeko1990



  • Members
  • Pip
  • 3 posts

Posted 06 May 2013 - 09:25 AM

sorry to be a pain but a cant get either of the suggestions to work. could some one be able to put a step by step for me i know i am asking a lot.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users