Jump to content

[SOLVED] Checking the database for duplicates?


Epidemic

Recommended Posts

I am making a registration form using PHP. This is what I have got so far.

 

// Get values from form
$username=$_POST['username'];
$password=$_POST['password'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(username, password)VALUES('$username', '$password')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
header("location:index.php?r=1");
}

else {
header("location:index.php?r=2");
}

// close connection
mysql_close();

 

However this script doesnt check for multiple inputs so if two people sign up with the same username it cannot check. How can I check if the username is already in use and if it is how can I get it to direct to index.php?r=3

 

Thanks,

 

Josh

Link to comment
Share on other sites

<?php
function checkUnique($table, $field, $compared){
if (get_magic_quotes_gpc()) {
$table = stripslashes($table);
$field = stripslashes($field);
$compared = stripslashes($compared);
}
$table = mysql_real_escape_string($table);
$field = mysql_real_escape_string($field);
$compared = mysql_real_escape_string($compared);

$result = mysql_query("SELECT $field FROM $table WHERE $field = '$compared'");
if(mysql_num_rows($result)==0) {
return TRUE;
}
else {
return FALSE;
}
}
?>

Link to comment
Share on other sites

I tried this but it didn't work.

 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$username=$_POST['username'];
$password=$_POST['password'];

//Account check
$sql="SELECT count(*) FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);

//Check to see if username exists or not.
if($result > 0){
header("location:register.php?r=1");
}

// Insert data into mysql
$sql="INSERT INTO $tbl_name(username, password)VALUES('$username', '$password')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
header("location:index.php?r=1");
}

else {
header("location:index.php?r=2");
}

// close connection
mysql_close();
?>

 

Any idea's on why?

Link to comment
Share on other sites

The problem is in the following line

//Check to see if username exists or not.
if($result > 0){
header("location:register.php?r=1");
}

$result is a php result resource and dont have any straight forward numeric or text value. You have to process this variable to get what you desire. In this case, you should write

 

//Check to see if username exists or not.
if(mysql_num_rows($result) > 0){
header("location:register.php?r=1");
}

 

Link to comment
Share on other sites

Sorry for the double post but when I uploaded it to my webhosting from my computer it stopped working. It now just goes to:

 

register.php?r=1

 

Whenever you try to register with a username that hasn't been used.

 

I do know that the PHP versions have changed from my server to the server I uploaded to. They changed from 5.2.6 to 5.2.5

 

Link to comment
Share on other sites

That would be because you are selecting a row number, then grabbing the number of rows. It will always return one row. You need to check the value of that row:

 

$sql="SELECT count(*) FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);
$num = mysql_result($result,0);
if($num > 0){
    header("location:register.php?r=1");
    exit();
}

Link to comment
Share on other sites

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

//Account check
$sql="SELECT count(*) FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);
$num = mysql_result($result,0);

//Check to see if username exists or not.
if($num > 0){
header("location:register.php?r=1");
exit();
}

// Insert data into mysql
$sql="INSERT INTO $tbl_name(username, password)VALUES('$myusername', '$mypassword')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
header("location:index.php?r=1");
}

else {
header("location:index.php?r=2");
}

// close connection
mysql_close();
?>

 

Thats the complete code, it doesn't contain mySQL user info (for obvious reasons :P)

Link to comment
Share on other sites

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

//Account check
$sql="SELECT count(*) FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);
$num = mysql_result($result,0);

//Check to see if username exists or not.
if($num > 0){
header("location:register.php?r=1");
exit();
}

// Insert data into mysql
$sql="INSERT INTO $tbl_name(username, password)VALUES('$myusername', '$mypassword')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
header("location:index.php?r=1");
}

else {
header("location:index.php?r=2");
}

// close connection
mysql_close();
?>

 

Thats the complete code, it doesn't contain mySQL user info (for obvious reasons :P)

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.