airbrushtutor Posted December 30, 2012 Share Posted December 30, 2012 Hi Freaks! I'm writing my first script from scratch.. best way to learn from everyone's advice i've seen. My goal is to create a form whereby an individual inputs a username and email (these are then put into the variables $username and $email using the $_POST superglobal. The part i'm stuck on is trying to sort through an existing database and check if the username exists, then i'd like to check if the email matches. All i'm trying to do is validate that they already exist in an existing table. If they don't, then i want to echo a message that says 'you must already be a member'. The following is only a snippet of the code - the web form and previous mentioned variables are both working fine. <?php //Now check to see if these details are correct as per the airbrushforum database //Connect to the database $dbc = mysqli_connect('localhost', 'XXXXX', 'PWgoesHere', 'database name') or die('Error connecting to MySQL server'); //Create the query and select all fields from the 'user' table $query = "SELECT * FROM user"; $result = mysqli_query($dbc, $query) or die('Error querying database'); //Create a row to loop through all results $row = mysqli_fetch_array($result); //Use a conditional statement to validate the email address and username - if either is incorrect then //echo a message //the following code is executing and echoing a list - how do i instead sort through the data to check //the $username and $email variable against what is in the database? do { echo $row['userid'] . ' ' . $row['email'] . '<br/>'; } while ($row = mysqli_fetch_array($result)); ?> //This is the basic conditional statement i need to check my data. (right?) if ($row['username'] == $username && $row['email'] == $email) { //Execute the rest of the code here } else { //echo that they must be a member of the airbrushforum. } Can't figure out if i should be using a foreach loop? if i should be looping a conditional statement? advice? If i've been unclear with anything i'm sure you'll let me know but please have patience with me, I don't seem to be picking up PHP as fast as i'd like to.. Quote Link to comment https://forums.phpfreaks.com/topic/272520-my-first-script/ Share on other sites More sharing options...
Barand Posted December 30, 2012 Share Posted December 30, 2012 (edited) When you reach the end of your while() loop, $row is set to false and so is not available once the loop exits. Also no need to select all the records, just select the the one WHERE username = '$username'. And do not use SELECT * unless you really do need every field. You are only interested in the email. so $sql = "SELECT email FROM user WHERE username = '$username' "; $res = mysqli_query($dbc, $sql); if (mysqli_num_rows($res) == 0) { echo "User $username not found"; } else { $row = mysqli_fetch_assoc($res); if ($row['email'] == $email) { // do something } else { // do something else } } Edited December 30, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/272520-my-first-script/#findComment-1402197 Share on other sites More sharing options...
airbrushtutor Posted December 31, 2012 Author Share Posted December 31, 2012 Thanks very much for that Barand - it's such a concise & simple answer yet i couldn't come up with that myself. I guess it's just practise. i haven't seen the 'mysqli_fetch_assoc' or ' mysqli_num_rows' functions before either so i'll have to check them out. I really appreciate your help, thank you for that Quote Link to comment https://forums.phpfreaks.com/topic/272520-my-first-script/#findComment-1402282 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.