user3191346
user3191346

Reputation: 9

Check if username or email is taken?

I understand that this may be a duplicate post, although I am unsure how to use the posts answers in my context. I am trying to post the registered fields to the database, but if they are taken (only username and email), return to the form where the user can then edit the wrong information. Could anyone help?

I am aware that I am using deprecated tags, this is for a college project

if (isset($_POST['firstname'], $_POST['surname'], $_POST['email'], $_POST['username'], $_POST['password'], $_POST['interest'])){

 $firstname = ($_POST['firstname']);
 $surname = ($_POST['surname']);
 $username = ($_POST['username']);
 $password1 = ($_POST['password']);
 $email = ($_POST['email']);
 $interest = ($_POST['interest']);

 $result = mysql_query("INSERT INTO user (firstname,surname,username,password,email,interestarea,user_type) VALUES ('$firstname','$surname','$username','$password1','$email','$interest','normal')");

 }
 if (isset($_POST['submit']))
{
?>
    <script>window.location='redirect.html';</script>
<?php
}
?>

I did have something like this, but this does not work

 $query2="SELECT * FROM user WHERE username='$username'";
 $result2=mysql_query($query2);
 $num=mysql_numrows($result2);


 if ($num!=0)
 {
 echo "That username has already been taken, please try again<br>
 <a href='register.php'>Register</a>";
 exit();
 }

Upvotes: 0

Views: 118

Answers (1)

putvande
putvande

Reputation: 15213

The function is mysql_num_rows instead of mysql_numrows:

$num = mysql_num_rows($result2);

mysql_num_rows

As the page also tells you, from PHP 5.5.0, this method can not be used anymore. So suggest start using mysqli_* or PDO

Upvotes: 3

Related Questions