van_t Posted March 15, 2007 Share Posted March 15, 2007 Hello I have a page that contains a form. I have a mysql db I have a query that pull out and displays a random value from one field within a table (from column b). On the form I have a text field that requires user input. I need to formulate some code that will use the If statement, get the current value from the user input and compare that value to column c of the table/record (above). Ex: User input is Blah Column b in table value = "what do people say when they speak?" column c in table value = "Blah" $result - 1st query is something like 'select question from questions order by rand() Limit 1"; $bquery - 2nd query is somehting like 'select answer FROM questions WHERE question=$result"; Im hoping the above will give $bquery the value of the answer on the same row as the $result question. Then In my page I want to use an IF statement to compare the user-input to the value of $bquery. If the values do not match, then to simply prevent the submission of the record. Otherwise it will submit the record to the DB. I already have everything working to submit to the database, but I cannot figure out how to use the IF to compare the values or to bypass the submission if the values do not match. Any help would be great Thank you Terry Link to comment https://forums.phpfreaks.com/topic/42866-php-if-_get/ Share on other sites More sharing options...
bwochinski Posted March 15, 2007 Share Posted March 15, 2007 First of all, you should assign your questions an ID as primary key, so you don't have to match against text fields. Assuming you do that then something like this for checking the answer: <?php if ( isset($_POST['answer']) AND is_numeric($_POST['q_id']) ) { $answer = mysql_result(mysql_query("SELECT answer FROM table_questions WHERE question_id=".$_POST['q_id']." LIMIT 1"),0,0); if ( strtolower($answer) == strtolower($_POST['answer']) ) { // Correct answer! Do processing here } else { // Incorrect answer. Display message. } } else { // No answer or invalid submission. } ?> Link to comment https://forums.phpfreaks.com/topic/42866-php-if-_get/#findComment-208171 Share on other sites More sharing options...
van_t Posted March 16, 2007 Author Share Posted March 16, 2007 I am a newbie to PHP and I am not sure how to impliment this script. If the answer is correct, then It needs to simply continue on to the submission (where the submission accesses another php page where it does inserts on the other fields. I do not want to insert the values from the questions/answers. I have a form <form name="formname" method="post" action="inserttodb.php"> If the answer is wrong, this will not happen, but to display a text message on the page saying the answer is wrong. If the answer is correct, it will continue on to the inserttodb.php page where the other values in the form will get submitted. If you would like for me to post the scripting, please let me know. Thank you Terry Link to comment https://forums.phpfreaks.com/topic/42866-php-if-_get/#findComment-208761 Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 I am a newbie to PHP If I had a dollar for every time I saw that in this forum. <?php if ( isset($_POST['answer']) AND is_numeric($_POST['q_id']) ) { $answer = mysql_result(mysql_query("SELECT answer FROM table_questions WHERE question_id=".$_POST['q_id']." LIMIT 1"),0,0); if ( strtolower($answer) == strtolower($_POST['answer']) ) { include('/actual/path/to/file/inserttodb.php'); } else { print "You did not do the correct answer"; } } else { // No answer or invalid submission. } ?> Link to comment https://forums.phpfreaks.com/topic/42866-php-if-_get/#findComment-208780 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.