Query used in checking existing users not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Query used in checking existing users not working

if( empty($errors)){ //if errors free $password_encrypt = md5($password); $query = "INSERT INTO `users`(user_id,firstname, lastname, password, email, phone_num) VALUES (default,'$firstname','$lastname','$password_encrypt','$emailaddress', '$phonenumber')"; $run_query = mysqli_query($dbconn,$query); //we need to check if a user does not already exist with the same email and or phone number $check_existing_user = "SELECT * FROM `users` WHERE email = '$emailaddress' AND phone_num = '$phonenumber'"; $result = mysqli_query($dbconn, $check_existing_user); if($result){ $numofrows = mysqli_num_rows($result); if($numofrows > 0){ echo "address or phone number already exist"; } } if($run_query == TRUE){ header('Location: registersuccess.php'); } else{ echo mysqli_error(); }

15th May 2019, 7:07 AM
Adetunji Sulaimon Tomiwa
Adetunji Sulaimon Tomiwa - avatar
6 Answers
+ 4
Yes, check out the link. Also, it's usually good to check if username and email are the same. If you want to make sure the phone number is unique, maybe you should make that field unique. http://www.mysqltutorial.org/mysql-unique/
15th May 2019, 7:38 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 3
What errors are you getting? I would avoid writing user_id and default if it's auto_increment. For reference: https://codewithawa.com/posts/complete-user-registration-system-using-php-and-mysql-database
15th May 2019, 7:21 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
0
@dplusplus am actually not getting any errors but I noticed that it was accepting existing emails and phone number into the db after running the query
15th May 2019, 7:24 AM
Adetunji Sulaimon Tomiwa
Adetunji Sulaimon Tomiwa - avatar
0
dplusplus
15th May 2019, 7:32 AM
Adetunji Sulaimon Tomiwa
Adetunji Sulaimon Tomiwa - avatar
0
Is that query the best query for checking existing users
15th May 2019, 7:33 AM
Adetunji Sulaimon Tomiwa
Adetunji Sulaimon Tomiwa - avatar
0
Thanks a lot. I will get back to you
15th May 2019, 7:46 AM
Adetunji Sulaimon Tomiwa
Adetunji Sulaimon Tomiwa - avatar