PHP PDO OOP:为什么只有一个值被发送/插入MySQL?

时间:2017-08-04 03:59:08

标签: php mysql pdo

Hye伙计们,

我真的需要帮助..我设法将此值提交到数据库,但它只提交一个,而不是所有值。下面我附上了这个例子。我输入了dd,55但是在DB中只输了55 I had enter dd, 55 but in DB only received 55

这就是我所做的。 我有这个HTML textarea: <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..";
  }

当我回声时它显示正确的值。然后我想将它提交到DB,这就是我所做的:

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;
    }   
}

1 个答案:

答案 0 :(得分:2)

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打印正确的结果是因为它在循环内部,将其打印到外部并且您将仅获取最后一个值,因此使用上面的代码,它将解决您的问题