0

I'm new to PHP and am practicing by programming a very basic signup form, which dumps the info into a MySQL database. However, I'm having a problem where, even if the name is taken, my program still adds the account to the SQL table. This is my code to check if the name exists:

$sql = "SELECT * FROM accounts WHERE name = '$name'";
$result = $conn->query($sql);

if (mysql_fetch_array($result) === true) {
     die("Error: Username has been taken");
} else {
//register account
}

Every time I run this, no matter what the name is, the program runs the block of code inside the else statement. I can provide more info if needed.

2 Answers2

0

You can alter the table and add make the field unique.

0

if you are using mysqli then use.

if ($result->num_rows > 0) { 
 die("Error: Username has been taken"); 
}
else {
// register here;
}

P.S do not use mysql read here.

Noman
  • 4,088
  • 1
  • 21
  • 36