pcw Posted February 24, 2009 Share Posted February 24, 2009 Hi, this script is the action of a form I have. I need it to match the gen_id that was submitted to that what is listed in the database. If it matches, I want it to allow the user to proceed and then show an error if it doesnt. This is what I have got. Currently it just says cannot query. Any help is much appreciated. <?php # I have added these variables, as this is what will be submitted by the form. $username = "paul123"; $gen_id = "JDWYK"; # This is the script $db = "moveitho_sitebuilder"; $link = mysql_connect('localhost', 'moveitho_paul', 'test'); if ( ! $link ) { $dberror = mysql_error(); return false; } if ( ! mysql_select_db( $db, $link ) ) { $dberror = mysql_error(); return false; } $result = mysql_query("SELECT username, gen_id FROM users WHERE username='%s' AND gen_id='%s'"); if ($result == $username && $gen_id) { print "PIN matched"; } else { die('Could not query:' . mysql_error()); } mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/ Share on other sites More sharing options...
trq Posted February 24, 2009 Share Posted February 24, 2009 Your not actually using the posted variables in your query amongst other vaious errors. <?php if (isset($_POST['submit'])) { // collect values from form. $username = mysql_real_escape_string($_POST['username']); $gen_id = mysql_real_escape_string($_POST['gen_id']); # This is the script $db = "moveitho_sitebuilder"; mysql_connect('localhost', 'moveitho_paul', 'test') or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) { if (mysql_num_rows($result)) { echo "PIN matched"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/#findComment-770636 Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 u have lot of things to take care there.... $result = mysql_query("SELECT username, gen_id FROM users WHERE username='%s' AND gen_id='%s'"); if ($result == $username && $gen_id) { please read manual for - how to fire mysql query - how to fetch a row from database ( this is different than firing a query) - if loop syntax. Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/#findComment-770642 Share on other sites More sharing options...
pcw Posted February 25, 2009 Author Share Posted February 25, 2009 Hi, thanks for your reply. I had to change the script slightly as it resulted in errors unless the database was connected to first. The only thing I am having trouble with now is showing a 'PIN successful' or 'PIN error' message. This is what I have got now: if (isset($_POST['submit'])) { // collect values from form. $link = mysql_connect('localhost', 'moveitho_paul', 'test'); if ( ! $link ) { $dberror = mysql_error(); return false; } if ( ! mysql_select_db( $db, $link ) ) { $dberror = mysql_error(); return false; } $username = mysql_real_escape_string($_POST['username']); $gen_id = mysql_real_escape_string($_POST['gen_id']); if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) { if (mysql_num_rows($result)) { echo "PIN matched"; } else { echo "PIN did not match"; } } It either shows an error for an unexpected else, or just shows the PIN did not match. Does anyone know how to fix this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/#findComment-770907 Share on other sites More sharing options...
pcw Posted February 25, 2009 Author Share Posted February 25, 2009 Can anyone help on this one? I have changed the code around and cant seem to get the right result from it. Here is what I have got now: if (isset($_POST['submit'])) { // collect values from form. $db = "moveitho_sitebuilder"; mysql_connect('localhost', 'moveitho_paul', 'test') or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); $gen_id = mysql_real_escape_string($_POST['gen_id']); if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) { if ( ! mysql_query($result)) { echo "PIN matched"; } else { echo "PIN did not match"; } } } Help is much needed and very appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/#findComment-770963 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 You are querying the result, which is probably throwing an error I hope. if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) { if (mysql_num_rows($result) > 0) { echo "Pin Matched"; }else { echo "Pin did not match."; } }else{ echo "SQL Error: " . mysql_error(); } Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/#findComment-771029 Share on other sites More sharing options...
pcw Posted February 25, 2009 Author Share Posted February 25, 2009 Hi premiso, thanks for your reply. I have now got the following code, but it just states that PIN did not match no matter what the entry is. Any idea on what is wrong? if (isset($_POST['submit'])) { $db = "moveitho_sitebuilder"; mysql_connect('localhost', 'moveitho_paul', 'test') or die(mysql_error()); mysql_select_db( $db) or die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); $gen_id = mysql_real_escape_string($_POST['gen_id']); if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) { if (mysql_num_rows($result) > 0) { echo "Pin Matched"; }else { echo "Pin did not match."; } }else{ echo "SQL Error: " . mysql_error(); } } Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/#findComment-771036 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 Echo out the username and gen_id, make sure that they are what you expect them to be from the $_POST code. Chances are something is slightly different and causing the issue. Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/#findComment-771056 Share on other sites More sharing options...
pcw Posted February 25, 2009 Author Share Posted February 25, 2009 premiso, you are a star! thank you It was just me being dopey. I forgot to carry $username as a hidden variable in the form. All works ok now Quote Link to comment https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/#findComment-771073 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.