PHP插入HTML表单值通知数组到字符串转换

时间:2017-05-21 05:46:44

标签: php mysql arrays pdo

当我尝试将表单值插入数据库时​​,我收到通知:

Array to string conversion in C:\path\rangking.inc.php on line 41

我已阅读this answer,但我有一个与print_r显示的不同数组:

Array ( [ia] => 6 [ik] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) [nn] => Array ( [0] => 80 [1] => 79 [2] => 79 [3] => 80 ) ) 1

任何建议的答案都会受到赞赏

表单HTML:

    if($_POST){

    include_once 'includes/rangking.inc.php';
    $eks = new rangking($db);

    $eks->ia = $_POST['ia'];
    $eks->ik = $_POST['ik'];
    $eks->nn = $_POST['nn'];

    if($eks->insert2()){
?>
<div class="alert alert-success alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <strong>Berhasil Tambah Data!</strong> Tambah lagi atau <a href="rangking.php">lihat semua data</a>.
</div>
<?php
    }

    else{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <strong>Gagal Tambah Data!</strong> Terjadi kesalahan, coba lagi.
</div>
<?php
    }
    }
?>
<form method="post">
  <div class="form-group">
    <label for="ia">Alternatif</label>
    <select class="form-control" id="ia" name="ia">
        <?php
            $stmt3 = $pgn1->readAll();
            while ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)){
                extract($row3);
                echo "<option value='{$id_alternatif}'>{$nama_alternatif}</option>";
            }
        ?>
    </select>
  </div>
  <div class="form-group">
        <?php
            $stmt2 = $pgn2->readAll();
            while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
                extract($row2);
        ?>
        <label for="ik"><?php echo $nama_kriteria; ?></label>
        <input type="hidden" name="ik[]" id="ik" value=<?php echo $id_kriteria ?>>
        <input type="text" class="form-control" id="nn" name="nn[]">
        <?php
            }
        ?>
  </div>
  <button type="submit" class="btn btn-primary">Simpan</button>
  <button type="button" onclick="location.href='rangking.php'" class="btn btn-success">Kembali</button>
</form>
<?php (print_r($_POST)); ?>

代码rangking.inc.php

    function insert2(){

        $query = "insert into ".$this->table_name." values(?,?,?,'','')";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->ia);
        $stmt->bindParam(2, $this->ik);
        $stmt->bindParam(3, $this->nn);

        if($stmt->execute()){
            return true;
        }else{
            return false;
        }

    }

修改

mixing function code of _chris85_ and mine

$query = "insert into ".$this->table_name." values(?,?,?,'','')";
foreach ($this->ik as $key => $value){
    $stmt = $this->conn->prepare($query);
    $stmt->bindParam(1, $this->ia);
    $stmt->bindParam(2, $value);
    $stmt->bindParam(3, $this->nn[$key]);
    if($stmt->execute()){
        return true;
    }else{
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

由于nnik是数组,因此需要循环遍历它们并绑定每个值。假设nnik具有一对一的关系,这是一个粗略的例子,说明如何做到这一点。

$query = "insert into ".$this->table_name. " values";
error_log('IK' . print_r($this->ik, 1));
foreach($this->ik as $key => $value) {
     error_log('In Loop');
     $query_bits[] = '(?, ?, ?, "", "")';
     $params[] = $this->ia;
     $params[] = $value;
     $params[] = $this->nn[$key];
}
$stmt = $this->conn->prepare($query . implode(',' $query_bits));
$stmt->execute($params);

这是一个粗略的,未经测试的样本,应该如何运作。

不要将变量放入$query_bits,否则你将失去准备/绑定的目的。

此方法在大型insert语句中生成,每行的数据用逗号分隔。

  

使用VALUES语法的INSERT语句可以插入多行。为此,请包含多个列值列表,每个列值括在括号内并用逗号分隔。

- https://dev.mysql.com/doc/refman/5.7/en/insert.html