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

<?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;
}
}
?>

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?

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");
}

 

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

 

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();
}

// 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)

// 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)

Archived

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

×
×
  • 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.