<?PHP
session_start();
if (isset($_SESSION['hasVoted']) && $_SESSION['hasVoted'] == '1') {
print "You've already voted";
}
else {
if (isset($_POST['Submit1'])) {
$ID = $_SESSION['user']; // obtain the ID of the user from the login session
var_dump($ID);
$string_results = $_POST['h2'];
$selected_radio = explode(',', $string_results);
var_dump($selected_radio);
$user_name = "root";
$password = "";
$database = "surveyTest";
$server = "127.0.0.1";
$SQL = "SELECT * FROM tblquestions";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$result = mysql_query($SQL);
$numRows = mysql_num_rows($result); //return number of rows in the table
var_dump($numRows);
$qNum = 'q1';
for ($i = 0; $i < $numRows; $i++)
{
var_dump($qNum);
$_SESSION['hasVoted'] = '1';
$selected_Value = $selected_radio[$i];
var_dump($selected_Value);
//==================================================================================
// SET Multiple rows IN THE answers TABLE for each question for a given student.
//==================================================================================
$SQL = "INSERT INTO answers (QID, Answer, SID) VALUES ('$qNum', '$selected_Value', '$ID')";
$result = mysql_query($SQL);
$question_Number = ltrim($qNum,'q');
$question_Number++;
$qNum ='q'.$question_Number;
}
mysql_close($db_handle);
print "Thank you for participating!";
}
else {
print "database error";
}
}
else {
header("location:login.php");;
}
}
session_destroy();
?>
<html>
<head>
<title>Process Survey</title>
</head>
<body>
</body>
</html>
当我执行上述代码时,包含 INSERT 的 $SQL 语句只执行一次。这意味着它只向数据库添加一条记录。但是,我需要它向数据库中添加几条等于 $numRows 的记录。$qNum 和 $selected_Value 的打印结果显示正确。有什么建议么?