...

View Full Version : Prevent duplicate content in database does NOT work!



angelali
02-25-2012, 03:37 PM
I am having a big problem in MySQL and a part in my PHP coding as well.

I created a registration form, where the user will have to fill his names, email address, choose a username and so on. I do not want duplicate email address and username in my database, thus, if a user choose either an email address or a username which has already taken, he will be notified.

To prevent this duplication, I have set both the email address and username fields as UNIQUE KEY in MySQL.

My problems are:

Even by setting both the username and email address fields as UNIQUE KEY, it is not working as I can register using the same email address or username. How to solve this?

I have coded also to prevent this problem of duplicate so that the user will be notified to choose another email or username, but I am having a warning.

My PHP codes:


<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['fname']) && isset($_POST['lname'])&& isset($_POST['emailr']) && isset($_POST['user']) && isset($_POST['pass'])) {
//Assignng variables
$firstname = mysql_real_escape_string($_POST['fname']);
$lastname = mysql_real_escape_string($_POST['lname']);
$email = mysql_real_escape_string($_POST['emailr']);
$uname = mysql_real_escape_string($_POST['user']);
$pwd = mysql_real_escape_string($_POST['pass']);
$pmd= md5($pwd);
//Database
$connect = mysql_connect('localhost', 'root', '') or die ('Connection Failed');
mysql_select_db('registration', $connect) or die ('Connection Failed');

//Registration codes

if (empty($firstname) || empty($lastname) || empty($email) || empty($uname) || empty($pmd)) {
echo '<p class="error">All fields are required to fill!</p>';
return false;
} elseif (strlen($firstname) && (strlen($lastname) < '2')) {
echo '<p class="error">Invalid first name or last name!</p>';
return false;
} elseif (filter_var($firstname, FILTER_VALIDATE_INT) || (filter_var($lastname, FILTER_VALIDATE_INT))) {
echo '<p class="error">First name or last name cannot be integers!</p>';
return false;
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo '<p class="error">Email address not valid!</p>';
return false;
} elseif (strlen($uname) && (strlen($pmd) < '6' )) {
echo '<p class="error">Username or password must be minimum 6 characters!</p>';
return false;
} else {
$query = "INSERT INTO login (id, firstname, lastname, emailaddress, username, password) VALUES('', '$firstname', '$lastname', '$email', '$uname', '$pmd')";
mysql_query($query, $connect);
if (mysql_num_rows(mysql_query("SELECT * FROM login WHERE emailaddress = '$email' username = '$uname'"))) {
echo '<p class="fail">This email or username is already taken!</p>';
}
}
}
}
?>

The warning message I am getting:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\miniimagehosting\register.php on line 44

chose
02-25-2012, 08:48 PM
you can add next condition to username and email,

SELCT COUNT(1) FROM users WHERE username = $uname;
if the result is equal 0, you can pass this username

angelali
02-25-2012, 08:51 PM
you included only for username

chose
02-25-2012, 08:55 PM
yeah, it's just example
you can use that to username and this to mail

SELCT COUNT(1) FROM users WHERE email = $email;
or fallowing query to both

SELCT COUNT(1) FROM users WHERE username = $uname OR email = $email;

Microsuck
02-25-2012, 08:59 PM
I am having a big problem in MySQL and a part in my PHP coding as well.

I created a registration form, where the user will have to fill his names, email address, choose a username and so on. I do not want duplicate email address and username in my database, thus, if a user choose either an email address or a username which has already taken, he will be notified.

To prevent this duplication, I have set both the email address and username fields as UNIQUE KEY in MySQL.

My problems are:

Even by setting both the username and email address fields as UNIQUE KEY, it is not working as I can register using the same email address or username. How to solve this?

I have coded also to prevent this problem of duplicate so that the user will be notified to choose another email or username, but I am having a warning.

My PHP codes:


<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['fname']) && isset($_POST['lname'])&& isset($_POST['emailr']) && isset($_POST['user']) && isset($_POST['pass'])) {
//Assignng variables
$firstname = mysql_real_escape_string($_POST['fname']);
$lastname = mysql_real_escape_string($_POST['lname']);
$email = mysql_real_escape_string($_POST['emailr']);
$uname = mysql_real_escape_string($_POST['user']);
$pwd = mysql_real_escape_string($_POST['pass']);
$pmd= md5($pwd);
//Database
$connect = mysql_connect('localhost', 'root', '') or die ('Connection Failed');
mysql_select_db('registration', $connect) or die ('Connection Failed');

//Registration codes

if (empty($firstname) || empty($lastname) || empty($email) || empty($uname) || empty($pmd)) {
echo '<p class="error">All fields are required to fill!</p>';
return false;
} elseif (strlen($firstname) && (strlen($lastname) < '2')) {
echo '<p class="error">Invalid first name or last name!</p>';
return false;
} elseif (filter_var($firstname, FILTER_VALIDATE_INT) || (filter_var($lastname, FILTER_VALIDATE_INT))) {
echo '<p class="error">First name or last name cannot be integers!</p>';
return false;
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo '<p class="error">Email address not valid!</p>';
return false;
} elseif (strlen($uname) && (strlen($pmd) < '6' )) {
echo '<p class="error">Username or password must be minimum 6 characters!</p>';
return false;
} else {
$query = "INSERT INTO login (id, firstname, lastname, emailaddress, username, password) VALUES('', '$firstname', '$lastname', '$email', '$uname', '$pmd')";
mysql_query($query, $connect);
if (mysql_num_rows(mysql_query("SELECT * FROM login WHERE emailaddress = '$email' username = '$uname'"))) {
echo '<p class="fail">This email or username is already taken!</p>';
}
}
}
}
?>

The warning message I am getting:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\miniimagehosting\register.php on line 44

1. You need to make sure that mysql_connect() goes before mysql_real_escape_string().

2. You gave a boolean to mysql_num_rows, you need to give it a resource.



$result = mysql_query("SELECT * FROM login WHERE emailaddress = '$email' username = '$uname'");

if (mysql_num_rows($result)) {
echo '<p class="fail">This email or username is already taken!</p>';
}

angelali
02-25-2012, 09:11 PM
microsuck, it is not working, still got errors

Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\miniimagehosting\register.php on line 60

Microsuck
02-25-2012, 09:13 PM
Try this:



$sql = "SELECT * FROM login WHERE emailaddress = '$email' username = '$uname'";
$result = mysql_query($sql);

if (mysql_num_rows($result)) {
echo '<p class="fail">This email or username is already taken!</p>';
}

angelali
02-25-2012, 09:24 PM
I have successfully solved the problem... i thank all of you who have helped me... thank guys...

Microsuck
02-25-2012, 09:36 PM
What was your fix?

angelali
02-25-2012, 09:38 PM
$query = "INSERT INTO login (id, firstname, lastname, emailaddress, password) VALUES('', '$firstname', '$lastname', '$email', '$pp')";
mysql_query($query, $connect);
if (mysql_errno($connect)> 0).................................................................................................. ................................



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum