大家好,
我真的需要帮助..我设法将此值提交到数据库中,但它只提交一个值,而不是所有值。下面我附上例子。我输入了 dd, 55 但在 DB 中只收到 55
这就是我所做的。
我有这个 html 文本区域:
<td><textarea class="form-control" name="d3_CA[]"></textarea></td>
然后,当我单击提交按钮时,我会这样处理:
if(isset($_POST['save-report'])){
foreach($_POST['d3_CA'] as $value)
{
$report->d3_CA = $value;
echo $report->d3_CA;
}
}
if($report->registerD3()){
$successMSG ="Success ! Report <span style='color:red;'>"
.$_POST['reportNo']. "</span> has been created. ";
}
else{
$errMSG = "Opps ! problem..";
}
当我回显时它显示正确的值。然后我想将其提交到数据库中,这就是我所做的:
function registerD3(){
$query2 = "INSERT INTO " . $this->table_d3 . "
SET d3_CA=:d3_CA";
$stmt = $this->conn->prepare($query2);
$stmt->bindParam(":d3_CA", $this->d3_CA);
echo $this->d3_CA;
if($stmt->execute()){
return true;
}
else{
return false;
}
}
最佳答案
if(isset($_POST['save-report'])){
foreach($_POST['d3_CA'] as $value)
{
$report->d3_CA = $value;
echo $report->d3_CA;
if($report->registerD3()){
$successMSG ="Success ! Report <span style='color:red;'>"
.$_POST['reportNo']. "</span> has been created. ";
}
else{
$errMSG = "Opps ! problem..";
}
}
}
问题是最后一个值存储在 $report->d3_CA
中,而 echo 打印正确的结果是因为它在循环内部,在外部打印它,您将得到仅最后一个值,因此使用上面的代码即可解决您的问题
关于PHP PDO 面向对象编程 : Why only one value is being send/insert into MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45498062/