DamienRoche Posted August 5, 2008 Share Posted August 5, 2008 Hi guys. I am having a little trouble here. I have coded a very simple login script yet I'm having trouble displaying an error message or restricting access if the MySQL query does not exactly match the users input. Here is the part of the code I'm having trouble manipulating: $data = mysql_query("SELECT * FROM financialone WHERE id = '$unique_id' and First_Name = '$fname' ") or die(mysql_error()); $fetch = mysql_fetch_array( $data ); $output = $fetch['Surname']; Now the code works as when I type in the the details of the two columns that match the row, I can 'fetch' any data from that row. My problem is that when I do not type the correct details, nothing happens, it simply doesn't fetch the data. How can I have it so that someone is either directed to another page, restricted access to a page or the page is displayed again with error messages? I'm suspecting it's a simple if statement, yet I have no idea how to cod it. So basically how do I say to php and mysql: if { data matches; fetch data; } if{ data doesn't match; display error, redirect, restrict; } Thank you for any help with this. Regards, Damien. Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/ Share on other sites More sharing options...
budimir Posted August 5, 2008 Share Posted August 5, 2008 You can add mysql_num_rows and do an if statment like this $num = mysql_num_rows($data); if ($num >= 0){ do you're stuff and go in } else { do nothing and go out } Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608753 Share on other sites More sharing options...
DamienRoche Posted August 5, 2008 Author Share Posted August 5, 2008 Sadly, that hasn't worked. Here's what I did if ($num >= 0){ $fetch = mysql_fetch_array( $data ); } else { echo "Wrong Login Information"; } It didn't do anything though, the data just wasn't fetched. There's no error messages either, even though it's not retrieving data. Does anyone have any other solutions? I need a match if statement. like: if { $comlumn1 and $column2 retrieve data; login; } else{ echo "error logging in"; } Any other thoughts? Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608774 Share on other sites More sharing options...
adam84 Posted August 5, 2008 Share Posted August 5, 2008 budimir, suggestion should have worked. Did you try to echo out your query statement to make sure that the variables are being inserted correctly? Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608779 Share on other sites More sharing options...
DamienRoche Posted August 5, 2008 Author Share Posted August 5, 2008 I have no idea. Is there no other way to directly ask for the two columns to match? I'll keep trying with the above script and search around. Hopefully I'll get it sorted soon. Thanks for your input. Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608787 Share on other sites More sharing options...
adam84 Posted August 5, 2008 Share Posted August 5, 2008 echo "SELECT * FROM financialone WHERE id = '$unique_id' and First_Name = '$fname'"; and see what is printed to the screen Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608790 Share on other sites More sharing options...
DamienRoche Posted August 5, 2008 Author Share Posted August 5, 2008 Yes, now I know what you mean. It prints SELECT * FROM financialone WHERE id = 'myid' and First_Name = 'Damien' ------ I can then fetch data from any column in that row. But that isn't the problem. The problem is finding a way to say - if 'myid' and 'Damien' are not BOTH present in that row THEN fail. Any ideas? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608793 Share on other sites More sharing options...
adam84 Posted August 5, 2008 Share Posted August 5, 2008 change this if ($num >= 0){ $fetch = mysql_fetch_array( $data ); } else { echo "Wrong Login Information"; } to if ($num > 0){ $fetch = mysql_fetch_array( $data ); } else { echo "Wrong Login Information"; } so if 1 or row is returned fetch the data else if no row is returned print the error message Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608800 Share on other sites More sharing options...
DamienRoche Posted August 5, 2008 Author Share Posted August 5, 2008 That sorted it!! thank you so much for your time! Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608806 Share on other sites More sharing options...
adam84 Posted August 5, 2008 Share Posted August 5, 2008 Yea no problem Quote Link to comment https://forums.phpfreaks.com/topic/118295-solved-help-with-simple-login-script-php-amp-mysql/#findComment-608807 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.