+ 1

Please, am i doing anything wrong, every time it runs it updates the email even if it exists in the database.

$error = ''; $userid = 1; $rows = $db->query("SELECT user_login, user_email FROM users WHERE ID != :ID", array('ID' => $userid ) ); if($rows){ foreach ($rows as $row) { if( $row['user_login'] == $phoneno){ $error = "phone number already exists"; }elseif ($row['user_email'] == $email) { $error = "email address already exists"; } else{ $error = ''; } } } if( $error == '' ){ $result = $db->query("UPDATE users SET user_login = :user_login, user_email = :user_email WHERE ID = :user_id", array('user_login' => $phoneno, 'user_email' => $email, 'user_id' => $userid) ); if($result){ $messages = "Account updated"; }else{ $errors = "Update failed"; } }else{ $errors = $error; }

31st Jul 2020, 4:04 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar
12 Respostas
+ 1
$email is whatever the user types in as their email and have tried logging the values too when i tried it when echoing values like if statement in the foreach loop am getting two response of the if and the else statement, i presume am not meant to use foreach loop but don't reallly know whats the issue
31st Jul 2020, 5:15 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar
+ 1
I just noticed there is a fault in your logic. Your code will give no error if the values do not exist in the last record. A better way to do this is to rearrange the assignment to outside the loop. if($rows){ $error = ''; foreach ($rows as $row) { if( $row['user_login'] == $phoneno){ $error = "phone number already exists"; }else if ($row['user_email'] == $email) { $error = "email address already exists"; } } } This will work but will have poor performance in a large database. Instead of handling this logic with PHP, it is better to use SQL. Something like this SELECT id FROM `users` WHERE `user_login` = :phoneno OR `user_email` = :email If the row count > 0; return an error
1st Aug 2020, 9:21 PM
Ore
Ore - avatar
+ 1
You lost me
1st Aug 2020, 9:35 PM
Ore
Ore - avatar
0
I am trying to check if any user uses the email or phone number the user is trying to update with before updating in the database but the code keeps updating even when another user has the email or phone number, please, any help would be great thanks
31st Jul 2020, 4:08 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar
0
The problem is likely with your else if condition elseif ($row['user_email'] == $email) I can't tell until I see the full code and I know what is assigned to $email but you can debug this by logging the value of $email and $row['user_email'] and see if they are the same.
31st Jul 2020, 4:34 PM
Ore
Ore - avatar
0
What do you suggest i do, do you think breaking it down like this part: if( $row['user_login'] == $phoneno){ $error = "phone number already exists"; }else{ $error = ''; } if ($row['user_email'] == $email) { $error = "email address already exists"; } else{ $error = ''; }
31st Jul 2020, 5:24 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar
0
Okay thanks, how do i test the clause on the previous data of the user
1st Aug 2020, 9:34 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar
0
I'm talking about the id. The thing is, the user might want to change its email or phoneno and at point of updating the credential he or she may mistakenly enter someone email or phone number already in the database. So it should give error msg once it sees that
1st Aug 2020, 9:38 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar
0
Thanks in advance and thanks for the previous reply i really appreciate your concern
1st Aug 2020, 9:39 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar
0
I think you can keep doing it like you have been doing if($error='') { $result = $db->query(....); }
1st Aug 2020, 9:43 PM
Ore
Ore - avatar
0
Don't know if you get me
1st Aug 2020, 9:44 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar
0
Thanks, i think i grasp your method will try that
1st Aug 2020, 9:47 PM
Okanlawon Anuoluwapo
Okanlawon Anuoluwapo - avatar