我正在接受一项迷你项目的挑战,我正在努力研究解决方案几个小时没有进展。 我的数据库表中有一列,其内容是通过展开以前的表单输入(在表中用逗号分隔)生成的。现在我需要在表单中使用textarea来获取这些值的输入(来自用户)。这些输入将在数组中,具体取决于首先从db获取的内容数,然后存储在我的表中的另一列中。这里的问题是,每次我提交表单时,我都会得到一个未定义的索引通知,其中包含textarea字段中值的名称,即test-col []。 请参阅下面的代码:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'myDb');
$users_id = mysqli_real_escape_string($conn, $_GET['id']);
$res = mysqli_query($conn, "SELECT tests FROM bal WHERE users_id = '$users_id'");
if ($res){
while ($row = mysqli_fetch_array($res)){
$tests = explode(',', $row['tests']);
foreach($tests as $test){
if ($test ==""){
continue;
}
echo '<div class="test-res" style="margin-top:10px;">
<form action="" method="post" role="form" class="form-horizontal">
<div class="form-group">
<label for= "test-col" class="form-label col-md-2">'.$test.' test</label>
<div class="col-md-10">
<textarea class="form-control" rows="3" name="test-col[]" placeholder="Test result"> </textarea>
</div>
</div>
</form>
</div>';
'<br /> <br />';
}
}
}
echo '<form action="" method="post">
<button type="submit" class="btn btn-success col-md-offset-5" name="sub-res">Send Result</button>
</form>';
?>
//to insert textarea values in db
<?php
if(isset($_POST['sub-res'])){
$conn = mysqli_connect('localhost', 'root', '', 'myDb');
foreach ($_POST ['test-col'] as $values){
$test_results = implode("<br>", $values);
}
$ins = mysqli_query($conn, "INSERT INTO bal (results) VALUES
('$test_results') WHERE users_id = '$users_id'");
if (!$ins){
die(mysqli_error());
}
else{
echo '<div class="alert alert-success">Successfully sent</div>';
}
}
?>
答案 0 :(得分:0)
您有两种形式 - 一种位于while
/ foreach
声明中,另一种位于下方。如果您要提交第二个表单,则不会包含第一个表单中的值。
改为在表单中换行;
<form action="" method="post">
<?php
$conn = mysqli_connect('localhost', 'root', '', 'myDb');
$users_id = mysqli_real_escape_string($conn, $_GET['id']);
$res = mysqli_query($conn, "SELECT tests FROM bal WHERE users_id =
'$users_id'");
if ($res){
while ($row = mysqli_fetch_array($res)){
$tests = explode(',', $row['tests']);
foreach($tests as $test){
if ($test =="") {
continue;
}
echo '<div class="test-res" style="margin-top:10px;"><div class="form-group">
<label for= "test-col" class="form-label col-md-2">'.$test.' test</label><div class="col-md-10"><textarea class="form-control" rows="3" name="test-col[]" placeholder="Test result"> </textarea></div></div></div>';
'<br /> <br />' ;
}
}
}
?>
<button type="submit" class="btn btn-success col-md-offset-5" name="sub-res">Send Result</button>
</form>