|
Posted by Jack Jackson on 08/03/05 14:53
Hi all. This has been an interesting week.
Now the form works, and I am able to error check, if no errors look into
user answer table and delete from that all q_ids matching the ones just
picked, insert this page of question/answer info (q_id/a_id) into the
user answer db, and if successful advance the category by 1 and get more
questions. It works.
I must have a logic error though because for some reason even though I
say to delete matching q_ids and reinsert, then move on, if the user
hits the back button, changes answers and hits submit again, the table
does not update . . . and the category does not increment. We get stuck
on this page of questions/answers forever.
What DOES happen after BACK is that the answers they select get passed
back to error check (eg if they select a select box to nothing, error
checking reenters its value into $message) and those values get passed
back to the display page (eg $_POST[$qname] == $aid) because the
"selected" answers change. It's just the db call and category advance
which get stuck.
Any help will be greatly appreciated - Here's the code:
<?php
//Start the session
session_start();
//Error announcements
echo "POST:<BR />";
print_r($_POST);
echo "<br /><br />";
echo "required session:<BR />";
var_dump($_SESSION['required_fields']);
echo "<br />\$ cat:" . $cat . "<br />";
echo "\$message: ";
var_dump($message);
//error_reporting(E_ALL);
/* A script to retrieve from database questions and answers,
* create a multi-page HTML form, error check answers and
* submit them to the database on a per-user basis.
* August 2005
*/
//Some basic vars
if (!isset($cat)) { $cat = "1"; }
$error=0;
$SUCCESS=0;
if (!isset($message))
{
$message = array();
}
if (!isset($_SESSION['required_fields']))
{
$_SESSION['required_fields'] = array();
}
if(!sizeof($_POST))
{
include_once(QUESTIONS . 'q.inc');
}
//error checking
reset($_SESSION['required_fields']);
foreach ($_SESSION['required_fields'] as $fieldname)
{
if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname]))
{
$message[$fieldname]=1;
}
}//error check
if (!empty($message))
{ $cat=$_POST['cat'];
include_once(QUESTIONS . 'q.inc');
}
//No errors? Store what's been done so far
if ( ($_POST['action'] == 'process') && (!sizeof($message) ) )
{
foreach($_POST as $key=>$val)
{
//find key/val sets within posts which are both numeric
if(is_numeric($key) && is_numeric($val))
{
$nkey=$key;
//add keys to the qidlist
$qidlist[] .= $key;
//add these values ( q_id, a_id ) to sql statement
$qanda[] .= "('1' , '" . $nkey . "' , '" . $val .
"')";
}
//find key/val sets within sub-arrays of $_POST
which are numeric
if(is_array($val))
{
foreach ($val as $akey=>$aval)
{
//add these values ( q_id, a_id ) to sql
statement
$qanda[] .= "('1' , '" . $key . "' , '" .
$aval . "')";
var_dump($qanda);
}
}
}
$qidlist_sql="DELETE FROM $userAnswers WHERE q_id IN ("
.. (implode(",",$qidlist)) . ");";
$q_a_sql="INSERT INTO $userAnswers (u_id, q_id, a_id )
VALUES " . (implode(",",$qanda)) . ";";
mysql_query($qidlist_sql);
if($q_a_result = mysql_query($q_a_sql))
{
unset($_SESSION['required_fields']);
$cat = $_POST['cat']+1;
include_once(QUESTIONS . 'q.inc');
}
else
{
echo "<b>A fatal MySQL error occured</b>.\n
<br />Query: " . $q_a_sql . "<br />\nError: (" .
mysql_error();
die;
}
}
?>
Navigation:
[Reply to this message]
|