gpt4 book ai didi

php - MySQL 代码只在循环中执行一次而不是多次

转载 作者:行者123 更新时间:2023-11-29 06:47:50 24 4
gpt4 key购买 nike

<?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 的打印结果显示正确。有什么建议吗?

最佳答案

使用 $result = mysql_query($SQL) or die(mysql_error()); 再试一次

你 table 上的主键也还好吗?

关于php - MySQL 代码只在循环中执行一次而不是多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com