unistake Posted July 26, 2010 Share Posted July 26, 2010 Hi all, I have a problem with an IF statement. I have two rows in my database with the same email address. Have a look at the code to see what I am trying to do! The problem is that the IF statement is only recognising the 1st row in my database, when I want it to recognise all the rows with the same email address. Hope you can help! Thanks <?php $sql = "SELECT reg FROM sales WHERE email='$_SESSION[logname]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query"); $row = mysqli_fetch_assoc($result); if ($row['reg'] != $_GET['reg'] ) { echo "I am sorry but '$_GET[reg]' does not seem to be registered by you!"; exit(); } if ($row['reg'] == $_GET['reg'] ) { $data = $row['reg']; } ?> Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 Your problem is that $row only contains one row of data from the db. What I would do is something like this: <?php $sql = "SELECT reg FROM sales WHERE email='$_SESSION[logname]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query"); $data = array(); //This way we can hold multiple results $i = 0; //The index of the array to add the result to while($row = mysqli_fetch_assoc($result)) { if ($row['reg'] != $_GET['reg'] ) { echo "I am sorry but '$_GET[reg]' does not seem to be registered by you!"; exit(); } else { //Why use two if statements? $data[$i] = $row['reg']; $i++; //increase the array index for the next while loop } } ?> Quote Link to comment Share on other sites More sharing options...
unistake Posted July 26, 2010 Author Share Posted July 26, 2010 Actually it doesnt seem to work, now the 2nd row is found but not the first ! The joys of PHP! <?php $sql = "SELECT reg FROM sales WHERE email='$_SESSION[logname]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query"); $data = array(); $i = 0; $row = mysqli_fetch_assoc($result); while($row = mysqli_fetch_assoc($result)) { if ($row['reg'] != $_GET['reg'] ) { echo "I am sorry but this aircraft '$_GET[reg]' does not seem to be registered by you!"; exit(); } else { $data[$i] = $row['reg']; $i++; } } } ?> Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 Sorry, remove the following line directly before the while loop $row = mysqli_fetch_assoc($result); Quote Link to comment Share on other sites More sharing options...
unistake Posted July 26, 2010 Author Share Posted July 26, 2010 still is not working, both rows are saying they are not registered with the email address. <?php $sql = "SELECT reg FROM sales WHERE email='$_SESSION[logname]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query"); $data = array(); //This way we can hold multiple results $i = 0; //The index of the array to add the result to while($row = mysqli_fetch_assoc($result)) { if ($row['reg'] != $_GET['reg'] ) { echo "I am sorry but this aircraft '$_GET[reg]' does not seem to be registered by you!"; exit(); } else { //Why use two if statements? $data[$i] = $row['reg']; $i++; //increase the array index for the next while loop } } ?> Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 Have you tried debugging by echoing both $_GET['reg'] and $row['reg'] for each line in the database? Those values must differ if you are getting your error message. Quote Link to comment Share on other sites More sharing options...
unistake Posted July 26, 2010 Author Share Posted July 26, 2010 When I echo $row['reg'] it returns both the row vaules together. How can they be separated? Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 I am not sure what you mean by both the row values together. while($row = mysqli_fetch_assoc($result)) { echo $row['reg'] . "<br>"; } The above code should output the values in the database, one on each line. These values should equal $_GET['reg'] for the script to work. Quote Link to comment Share on other sites More sharing options...
unistake Posted July 26, 2010 Author Share Posted July 26, 2010 for that echo you gave me it shows like this: row1value1 row2value2 Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 The code should give you exactly what is in the "reg" column of your database. If your database looks like this: id | reg ------------------ 1 | aaaa 2 | bbbb Then the output should be: aaaa bbbb Quote Link to comment Share on other sites More sharing options...
unistake Posted July 26, 2010 Author Share Posted July 26, 2010 yes, that is what it is showing, it was working for the first row before I started editing the code to get the second row to work. Now both rows are not working, i have not touched the database. Any more ideas?! Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 Maybe try swithing the if statement to if($row['reg'] == $_GET['reg']) and swap the error message etc respectively. If the two variables match each other, then the if statement should pick it up fine. If it doesnt, then I am out of ideas (in which case starting a new thread may help as people will probably think you got the help you needed on this one). Quote Link to comment Share on other sites More sharing options...
unistake Posted July 26, 2010 Author Share Posted July 26, 2010 Thanks a lot for your help Alex, I just tried doing that actually and it does not work. I will leave it for now and work on a different section for a bit! Thanks for your time Quote Link to comment 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.