I'm having problem with the script below.
The atitle getting the qid from the last question inserted in the table. I need to have atitle with the corresponding qid.
for example:
qtitle (qid=1)
- atitle (qid=1)
- atitle (qid=1)
qtitle (qid=2)
- atitle (qid=2)
- atitle (qid=2)
can anyone please help.. thanks in advance.
<?php
require_once('auth.php');
require_once('config.php');
$date=date("F j, Y, g:i a");
$stitle = $_POST['stitle'];
$stitle = ucwords($stitle);
$name = $_SESSION['SESS_NAME'];
$member_id = $_SESSION['SESS_MEMBER_ID'];
$query = "INSERT INTO survey (stitle, sdate, name, member_id) VALUES ('$stitle', '$date', '$name', '$member_id')";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
$sid = mysql_insert_id();
// reset variables
unset($query);
unset ($result);
foreach ($_POST['questions'] as $q) {
if (trim($q) != '') {
$qtitles[] = $q;
}
}
foreach ($qtitles as $qtitle) {
$query = "INSERT INTO questions (sid, qtitle) VALUES ('$sid', '$qtitle')";
$result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
}
$qid = mysql_insert_id();
// reset variables
unset($query);
unset ($result);
foreach ($_POST['options'] as $o) {
if (trim($o) != '') {
$atitles[] = $o;
}
}
foreach ($atitles as $atitle) {
$query = "INSERT INTO answers (qid, sid, atitle, acount) VALUES ('$qid', '$sid', '$atitle', '0')";
$result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
}
//Check whether the query was successful or not
if($result) {
header("location: surveypreview.php");
exit();
}else {
echo '<script>alert("Query Failed. Please try again !!!");</script>';
echo '<script>history.back(1);</script>';
}
mysql_close();
?>