Jump to content

[SOLVED] Please help! Why doesn't questions 12-17 enter in MYSql?


chancellor

Recommended Posts

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
?>

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.