markifornia Posted April 5, 2012 Share Posted April 5, 2012 On page 274, it explains why the query is written this way, the reason below: "To write one query that will work for both possibilities, don't check to see if the email address is being used, but rather see if it's being used by anyone else..." On Line 53: $q = "SELECT user_id FROM users WHERE email='$e' and user_id != $id"; I don't see how this code is checking to see if being used by anyone else, I'm confused with this query. Is there an easier way this could be explained or elaborated more for better comprehension? Thanks, Mark Link to comment Share on other sites More sharing options...
Paul Swanson Posted April 5, 2012 Share Posted April 5, 2012 That query will SELECT a user_id if the email address is found in the users table only if the user_id NOT (!=) the same as the current user. So if a dataset is returned, it would indicate that another user_id is already using that email address. 1 Link to comment Share on other sites More sharing options...
markifornia Posted April 5, 2012 Author Share Posted April 5, 2012 What an interesting query, it took a while to get my head around it. I could almost write up so many different versions of how it could be explained, at least for myself. Nonetheless, you've explained it clearly, thanks Paul! ... Line 60 if (mysql_affect_rows($r) == 0 ) { // The query did not find another email that matches the current users email address // UPDATE query } else { // The query found another email address in the database that matches the current users email address // already registered } Link to comment Share on other sites More sharing options...
bahaa Posted April 8, 2012 Share Posted April 8, 2012 Here is 2 functions to check for unique email address, one used for insert and the other for update when you try to update a record and want to check for unique email, you have to check for all records except the one you are trying to use function uniqueEamilUpdate($email, $ID){ global $sdbc; $query = "SELECT userEmail, userID FROM user WHERE userEmail ='". mysqli_real_escape_string($sdbc, $email)."' AND userID !='". mysqli_real_escape_string($sdbc, $ID)."'"; $result = mysqli_query($sdbc, $query); return $result; } and here is a function to check for unique email on insert function uniqueEamil($email){ global $sdbc; $query = "SELECT userEmail, userID FROM user WHERE userEmail ='". mysqli_real_escape_string($sdbc, $email)."'"; $result = mysqli_query($sdbc, $query); return $result; } Link to comment Share on other sites More sharing options...
markifornia Posted April 9, 2012 Author Share Posted April 9, 2012 Thanks bahaa, what is assiged to $sdbc? Is it the below: mysqli_query($dbc, $q); Also, is the function missing another parameter of $ID? function uniqueEmail($email) Very useful functions, I've never created functions with query statements, I like this idea. It's awesome. Link to comment Share on other sites More sharing options...
bahaa Posted April 9, 2012 Share Posted April 9, 2012 $sdbc is my connection. The function is not missing any thing, I posted 2 functions, the one without id is used when sign in up for new user and in this case there is no id to check for. the function that takes ID as a parameter is used when for updating. Link to comment Share on other sites More sharing options...
markifornia Posted April 9, 2012 Author Share Posted April 9, 2012 Ah, sorry misread. I do see now that the second function is just selecting the user_id. Link to comment Share on other sites More sharing options...
Recommended Posts