chancellor Posted January 15, 2008 Share Posted January 15, 2008 In the following code, I have a problem when the test goes to using text boxes instead of multiple choice. The text entered in the text box does not enter into the MySql database. The sql string is correct because I can use it to enter it directly into the table. But, for some reason, once the code gets to question number 12, $result gives a result of "" and the data doesn't get entered in the table. I'm going blind looking at all the {} and ||! Please help <?php if(array_key_exists('page', $_POST) == FALSE) { $page = 0; } else { $page = $_POST['page']; } $connection=connection string (which I cannot post here for security reasons) if ($page<12) {// Check answer here $db_name="histotec_db2"; $db=mysql_select_db($db_name,$connection); $sql="select questionanswer from tabTrainingQuestions where questionnumber=". $page; $result=mysql_query($sql,$connection); if(!$result) mysql_error(); while ($row=mysql_fetch_array($result, MYSQL_NUM)) { foreach($row as $v); } } if ($page !=0){ $sql1="insert into TabTraining(user,guess,questionnum,useipaddress) values('{$_POST[user]}','$_POST[question]',{$page},'{$_POST[ipaddress]}')"; $result=mysql_query($sql1,$connection); print $connection."\n"; print $result."\n"; print $sql1."\n"; } if (($_POST["question"]==$v)|| ($page>11)){ $page=$page+1; } // Get next question $db_name="histotec_db2"; $table_name="tabTrainingQuestions"; $connection=connection string $db=mysql_select_db($db_name,$connection); $sql="select actualquestion from tabTrainingQuestions where questionnumber=". $page; $result=mysql_query($sql,$connection); $num_rows = mysql_num_rows($result); if (($num_rows==0) && ($page!=0)) { header("location: " . "finish15.php"); } if(!$result) mysql_error(); echo "<html>"; echo "<body>"; while ($row=mysql_fetch_array($result, MYSQL_NUM)) { foreach ($row as $v1) {print "{$v1}";} print "<BR>"; } if ($page<12){ if ($_POST["question"]!=$v){ print '<p style="color:red"> wrong answer <p style="color:black" >'; } } ?> <FORM METHOD="POST" ACTION="allinonechapter6test.php"> <?php echo "<input type=hidden name='user' value='{$_POST['user']}'>"; echo "<input type=hidden name='ipaddress' value='{$_POST['ipaddress']}'>"; $sql="select answerletter,answer,answernumber from tabAnswers where answernumber=". $page; $result=mysql_query($sql,$connection); if ($page>11){ print "<INPUT TYPE=TEXT NAME='question' ROWS=5 COLS=50 VALUE=''>\n"; } else{ while ($row=mysql_fetch_array($result,MYSQL_NUM)) { print "\n"; print "<INPUT TYPE=RADIO NAME='question' VALUE={$row[0]}>\n"; print "{$row[1]}\n"; print "<BR>"; } } ?> <input type='hidden' name='page' value='<?php echo $page; ?>' > <INPUT TYPE=SUBMIT VALUE="Submit Form"> </form> </body> </html> MOD EDIT: This time, I added the code tags for you. Use <?php // your code ?> Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/ Share on other sites More sharing options...
RIRedinPA Posted January 15, 2008 Share Posted January 15, 2008 Not sure why it's not posting but don't these POST variable requests have to be in quotes? $sql1="insert into TabTraining(user,guess,questionnum,useipaddress) values('{$_POST[user]}','$_POST[question]',{$page},'{$_POST[ipaddress]}')"; Shouldn't they be written like {$_POST[\'user\']} and why is $_POST[question] not formatted like the others (in between curly brackets)? Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440150 Share on other sites More sharing options...
chancellor Posted January 15, 2008 Author Share Posted January 15, 2008 If that were the issue, ALL of the questions shouldn't post and they do. Also, copying the SQL command directly into PHPmyadmin inserts correctly. Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440152 Share on other sites More sharing options...
revraz Posted January 15, 2008 Share Posted January 15, 2008 There is no way this SQL command would work ('{$_POST[user]}','$_POST[question]',{$page},'{$_POST[ipaddress]}')"; Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440154 Share on other sites More sharing options...
darkfreaks Posted January 15, 2008 Share Posted January 15, 2008 <?php if(array_key_exists('page', $_POST) == FALSE) { $page = 0; } else { $page = $_POST['page']; } $connection=connection string (which I cannot post here for security reasons) if ($page<12) {// Check answer here $db_name="histotec_db2"; $db=mysql_select_db($db_name,$connection); $sql="select questionanswer from tabTrainingQuestions where questionnumber=". $page; $result=mysql_query($sql,$connection); if(!$result) mysql_error(); while ($row=mysql_fetch_array($result, "MYSQL_NUM")) { foreach($row as $v); } } if ($page !=0){ $sql1="insert into TabTraining(user,guess,questionnum,useipaddress) values('{$_POST[user]}','$_POST[question]',{$page},'{$_POST['ipaddress']}')"; $result=mysql_query($sql1,$connection); print $connection."\n"; print $result."\n"; print $sql1."\n"; } if (($_POST["question"]==$v)|| ($page>11)){ $page=$page+1; } // Get next question $db_name="histotec_db2"; $table_name="tabTrainingQuestions"; $connection=connection string $db=mysql_select_db($db_name,$connection); $sql="select actualquestion from tabTrainingQuestions where questionnumber=$page"; $result=mysql_query($sql,$connection); $num_rows = mysql_num_rows($result); if (($num_rows==0) && ($page!=0)) { header("location: " . "finish15.php"); } if(!$result) mysql_error(); echo "<html>"; echo "<body>"; while ($row=mysql_fetch_array($result, "MYSQL_NUM")) { foreach ($row as $v1) {print "{$v1}";} print "<BR>"; } if ($page<12){ if ($_POST["question"]!=$v){ print '<p style="color:red"> wrong answer <p style="color:black" >'; } } ?> <FORM METHOD="POST" ACTION="allinonechapter6test.php"> <?php echo "<input type=hidden name='user' value='{$_POST['user']}'>"; echo "<input type=hidden name='ipaddress' value='{$_POST['ipaddress']}'>"; $sql="select answerletter,answer,answernumber from tabAnswers where answernumber=$page"; $result=mysql_query($sql,$connection); if ($page>11){ print "<INPUT TYPE=TEXT NAME='question' ROWS=5 COLS=50 VALUE=''>\n"; } else{ while ($row=mysql_fetch_array($result,MYSQL_NUM)) { print "\n"; print "<INPUT TYPE=RADIO NAME='question' VALUE={$row[0]}>\n"; print "{$row[1]}\n"; print "<BR>"; } } ?> <input type='hidden' name='page' value='<?php echo $page; ?>' > <INPUT TYPE=SUBMIT VALUE="Submit Form"> </form> </body> Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440155 Share on other sites More sharing options...
chancellor Posted January 15, 2008 Author Share Posted January 15, 2008 There is no way this SQL command would work ('{$_POST[user]}','$_POST[question]',{$page},'{$_POST[ipaddress]}')"; The sql command works on questions 1-11. IT is when I go from radio buttons to text box that it stops working. Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440161 Share on other sites More sharing options...
AndyB Posted January 15, 2008 Share Posted January 15, 2008 The sql command works on questions 1-11. IT is when I go from radio buttons to text box that it stops working. Note that in your code ALL of the radio buttons are named the same so you would never be able to retrieve the values anyway. Try naming them as array elements, i., name='question[]' and then you'll have discrete results. Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440162 Share on other sites More sharing options...
chancellor Posted January 15, 2008 Author Share Posted January 15, 2008 The sql command works on questions 1-11. IT is when I go from radio buttons to text box that it stops working. Note that in your code ALL of the radio buttons are named the same so you would never be able to retrieve the values anyway. Try naming them as array elements, i., name='question[]' and then you'll have discrete results. The radio buttons work fine. For some reason, the text boxes do not. This page posts to itself over and over again. One page and the questions rotate. Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440163 Share on other sites More sharing options...
kenrbnsn Posted January 15, 2008 Share Posted January 15, 2008 Do you have a sample URL where we could look at the problem? Ken Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440165 Share on other sites More sharing options...
chancellor Posted January 15, 2008 Author Share Posted January 15, 2008 Do you have a sample URL where we could look at the problem? Ken Unfortunately no. The problem is not immediately obvious on the website. You'd have to be able to look at the MySql data. Quote Link to comment https://forums.phpfreaks.com/topic/86182-solved-please-help-why-doesnt-questions-12-17-enter-in-mysql/#findComment-440168 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.