0

I tried reading some of other answers on this issue, but none of them are really answering everything, and I'm not even sure if this would be the best way to do it anymore.

What I'm trying to do here is to check if username exists and if password matches the second password entry. it all worked fine before testing if the username already existed, and in some other forms it actually works, so I believe I'm mixing mysqli and mysql functions, but I'm not sure which ones, please help me guys :)

$conn = mysql_connect("localhost", "root", "password") or die (mysql_error());
mysql_select_db("Accounts") or  die (mysql_error());

$checkusername = mysql_query("SELECT * FROM users2 WHERE username = '$username'") or die (mysql_error());
$count = mysql_num_rows($checkusername);

if (($password === $passwordTest) && ($count == 0)){

  $sql = "INSERT INTO users2 (Username, Password, First, Last)
  VALUES ('$username', '$password', '$firstName', '$lastName')";

 if ($conn->query($sql) === TRUE) {
echo "Data inserted successfully";
  }  

  else {
echo "Error creating Account: " . $conn->error;
  }
}

elseif ($password != $passwordTest){
  echo "Password not matching.";
}

if ($count > 0){
  echo "Username already exists.";
}

the error says the mistake is here:

if ($conn->query($sql) === TRUE)
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Yes, you're mixing APIs. Stick with `mysqli_` – Qirel Nov 25 '16 at 23:48
  • You are open to SQL injections. Passwords should be hashed. You shouldn't use the `mysql_*` functions anymore. Use parameterized queries with PDO or mysqli. – chris85 Nov 25 '16 at 23:48
  • I used the converter from the http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php and it helped a lot. I see that many people have trouble with this migration, simply because most of the tutorials are old and use msql API.. – arrivabene Nov 26 '16 at 22:21

0 Answers0