Jump to content

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


steeko1990

Recommended Posts

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:

 

 

 

 

<?php
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";
mysql_close($con);
?>
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
Link to comment
Share on other sites

<?php
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";}
else
{echo"Choose alternative username";}
?>

Hope this helps. 

Edited by RonnieCosta50
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.