Yazrihm Doe Yazrihm Doe - 1 year ago 83
PHP Question

PHP username already exists, when it doesn't

I've checked many threads about my problem, but it still won't work.

When I enter username, which doesn't exist, it will throw the username has been created yet. But, when I enter an username already registered in my db, it still register.

Here is my php/html page for register :

$error = false;
if(isset($_POST['signup'])) {

$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);

$sql = "SELECT name FROM users WHERE name='".mysql_real_escape_string($name)."'";
$query = mysqli_query($sql);
$error = true;
$ckname_error = "Nom d'utilisateur déjà enregistré!";
} else { unset($ckname_error); }

if (!$error) {
if(mysqli_query($con, "INSERT INTO users(name,mail,password,rank) VALUES('" . $name . "', '" . $email . "', '" . md5($password) . "', 'user' )")) {
$successmsg = "Successfully Registered! <a href='login.php'>Click here to Login</a>";
header("Location: login.php");
} else {
$errormsg = "Error in registering...Please try again later!";

Answer Source

You're getting unreliable results because you're mixing mysqli_ with mysql_ functions.

1. Change:

$sql = "SELECT name FROM users WHERE name='".mysql_real_escape_string($name)."'";


$sql = "SELECT name FROM users WHERE name='$name'";

2. Change

$query = mysqli_query($sql);


$query = mysqli_query($con,$sql);

3. Change



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download