Hello,
I am trying to build a test with PHP and mysql and can't solve this problem. I have a form with five answers and if the user selects the correct answer they get one point. If they select wrong they get zero points. I have it all tied into MySQL with user login sessions. When they arrive at the first test their id, username, and password are automatically entered into the database. There are 12 questions per test and there is one question per page. You hit the submit button and then move to the next page. The database then updates with the answer. However when I import the data from MySQL it is adding a new row for each question and filling in zero's for the rest.
For instance:
ID username password q1 q2 q3 etc.
1 john 45thy 1 0 0
0 1 0
0 0 1
I want it to do this:
ID username password q1 q2 q3 etc.
1 john 45thy 1 1 1
I want the database to have a username password and then the answers straight across but I just can't figure out how to get it to work. It is adding a new line in for each question basically. Here is a copy of the code and form for one question. I'm new at this so it is probably a simple problem but I am not skilled enough to find it. This is my first try so if there is a better way to go about this I am all ears.
Thanks.
$message="";
$numone=1;
$numtwo=0;
if (isset( $actionflag ) && $actionflag=="q1" )
{
mysql_query("INSERT INTO test ( id, username, password )
VALUES ( '$session[id]', '$session[username]', '$session[password]' )");
if ($calcE == "answerE") {
mysql_query("INSERT INTO test (questionone)
values ( '$numone' )");
}
else
{
mysql_query("INSERT INTO test (questionone)
values ( '$numtwo' )");
}
if (($calcA =="") && ($calcB =="") && ($calcC =="") && ($calcD =="") && ($calcE =="")) {
$message .="You forgot to answer the question!";
}
if ( $message == "" )
{
header("Location: /nextquestion.php?".SID );
}
}
and the HTML form:
<?php
if ( $submit = "Submit_Answer" )
{
print "<b>$message</B><p>";
}
?> </p>
</blockquote>
<form ACTION="<?php print $PHP_SELF;?>">
<input type="hidden" name="actionflag" value=http://www.phpbuilder.com/board/archive/index.php/"q1">
<input type="hidden" name="<?php print session_name() ?>"
value="<?php print session_id() ?>">
<blockquote>
<p> <font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcA" VALUE="answerA">
a. Heparin iv followed by warfarin</font> <br>
<font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcB" VALUE="answerB">
b. Warfarin alone</font> <br>
<font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcC" VALUE="answerC">
c. Aspirin </font><br>
<font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcD" VALUE="answerD">
d. Heparin iv followed by subcutaneous heparin at 5000 units twice a day
</font><br>
<font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcE" VALUE="answerE">
e. Heparin iv followed by a treatment dose of low molecular weight heparin
</font></p>
I am trying to build a test with PHP and mysql and can't solve this problem. I have a form with five answers and if the user selects the correct answer they get one point. If they select wrong they get zero points. I have it all tied into MySQL with user login sessions. When they arrive at the first test their id, username, and password are automatically entered into the database. There are 12 questions per test and there is one question per page. You hit the submit button and then move to the next page. The database then updates with the answer. However when I import the data from MySQL it is adding a new row for each question and filling in zero's for the rest.
For instance:
ID username password q1 q2 q3 etc.
1 john 45thy 1 0 0
0 1 0
0 0 1
I want it to do this:
ID username password q1 q2 q3 etc.
1 john 45thy 1 1 1
I want the database to have a username password and then the answers straight across but I just can't figure out how to get it to work. It is adding a new line in for each question basically. Here is a copy of the code and form for one question. I'm new at this so it is probably a simple problem but I am not skilled enough to find it. This is my first try so if there is a better way to go about this I am all ears.
Thanks.
$message="";
$numone=1;
$numtwo=0;
if (isset( $actionflag ) && $actionflag=="q1" )
{
mysql_query("INSERT INTO test ( id, username, password )
VALUES ( '$session[id]', '$session[username]', '$session[password]' )");
if ($calcE == "answerE") {
mysql_query("INSERT INTO test (questionone)
values ( '$numone' )");
}
else
{
mysql_query("INSERT INTO test (questionone)
values ( '$numtwo' )");
}
if (($calcA =="") && ($calcB =="") && ($calcC =="") && ($calcD =="") && ($calcE =="")) {
$message .="You forgot to answer the question!";
}
if ( $message == "" )
{
header("Location: /nextquestion.php?".SID );
}
}
and the HTML form:
<?php
if ( $submit = "Submit_Answer" )
{
print "<b>$message</B><p>";
}
?> </p>
</blockquote>
<form ACTION="<?php print $PHP_SELF;?>">
<input type="hidden" name="actionflag" value=http://www.phpbuilder.com/board/archive/index.php/"q1">
<input type="hidden" name="<?php print session_name() ?>"
value="<?php print session_id() ?>">
<blockquote>
<p> <font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcA" VALUE="answerA">
a. Heparin iv followed by warfarin</font> <br>
<font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcB" VALUE="answerB">
b. Warfarin alone</font> <br>
<font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcC" VALUE="answerC">
c. Aspirin </font><br>
<font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcD" VALUE="answerD">
d. Heparin iv followed by subcutaneous heparin at 5000 units twice a day
</font><br>
<font face="Georgia, Times New Roman, Times, serif">
<INPUT TYPE="radio" NAME="calcE" VALUE="answerE">
e. Heparin iv followed by a treatment dose of low molecular weight heparin
</font></p>