gpt4 book ai didi

php - 无法在php中保存来自textarea的多个用户输入

转载 作者:行者123 更新时间:2023-11-27 23:14:39 25 4
gpt4 key购买 nike

我已经从 lvl2itquepaper 检索数据并用 textarea 显示它并将用户输入保存到另一个表调用 lvl2itresult 但是当我按下保存时,它只保存最后一个用户输入。例如,有 2 个问题,有 2 个文本区域,第一个文本区域是“A”,第二个文本区域是“B”,它将两个用户输入都保存为“B”

             <?php
include('../dbconnect.php');
session_start();
?>



<!DOCTYPE html>
<html>

<head>
<title>Online Examination System</title>
</head>
<body>


<div id="container">
<h1>Level 2 IT Question Paper</h1>
<h2>Please read the question carefully and answer it confidently. Good Luck All!</h2>


<?php
if(isset($_POST['Submit']))
{
$sql="SELECT * from lvl1itquepaper";
$run_que = mysqli_query($mysqli, $sql);
$check_que = mysqli_num_rows($run_que);

while ($row=$run_que->fetch_assoc())
{
$questionno = $row['questionno'];
$question = $row['question'];
$student_ans = $_POST['studentans'];


$sql="insert into lvl2itresult (questionno, question, studentans, username) values ('.$questionno.', '$question', '$student_ans', '".$_SESSION['login_user']."')";

$submit = $mysqli->query($sql);


}
}
?>




<form method= "post">
<?php
echo "Welcome, ";
$sql="SELECT * from lvl2itstudent WHERE username= '".$_SESSION['login_user']."'";
$find_student = mysqli_query($mysqli, $sql);
$check_student = mysqli_num_rows($find_student);
if ($check_student>0){
while($row = $find_student->fetch_assoc())
{
echo $row['username'];
}
}
echo "<br><br><br><br>";

$sql="SELECT * from lvl2itquepaper";
$run_que = mysqli_query($mysqli, $sql);
$check_que = mysqli_num_rows($run_que);

if($check_que>0){
while ($row=$run_que->fetch_assoc())
{
$questionno = $row['questionno'];
$question = $row['question'];
echo "".$questionno. "." .$question."<br>";
echo "<textarea name='studentans' rows='5' cols='50'></textarea><br><br>";



}
}

else {
echo "there is no data in database";
}
?>
<input type="submit" value = "Submit" name= "Submit" style= "width:60px; height:30px";>

</form>
</div>
</body>

最佳答案

这很简单,但很难用几句话来解释。几乎所有的事情都是在 POST 数据中只发送了一个参数,因为没有设置array;此外,PHP 也不将信息视为数组,因此只有一个值需要解释。在任何情况下,如果数据发送正确,PHP 会将 POST 值识别为二维数组,并输出无法将数据解析为字符串的 SQL 错误。

要解决这个问题,首先更改您的 <textarea>标记如下:

<textarea name='studentans[]' rows='5' cols='50'></textarea>

括号会告诉 HTML 会有很多元素的名称是 studentans。 .

现在进入您的插入逻辑,您需要处理使用索引获取值的数组。通常这些将从 0 开始,因此我们将使用它:

$i = 0; // $i stands for index.
while ($row=$run_que->fetch_assoc()){
$questionno = $row['questionno'];
$question = $row['question'];
$student_ans = $_POST['studentans'][$i]; // this is where the magic happens

$sql="insert into lvl2itresult (questionno, question, studentans, username) values ('$questionno', '$question', '$student_ans', '".$_SESSION['login_user']."')";
// please also notice I deleted 2 concatenating dots near "values ('$questionno',"
$submit = $mysqli->query($sql);
$i++; // increment by 1 so we can access the next value on the next loop
}

那应该就可以了。我希望我没有忘记任何细节。

关于php - 无法在php中保存来自textarea的多个用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43875830/

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