0

I have a notice when I'm try to insert form values to database:

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

I've read this answer but I have a different array that showing with print_r is:

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

any suggesting answer would be appreciate

form 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)); ?>

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

    }

EDIT

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;
    }
}
2
  • i'm trying to insert the values of HTML form into database, but on HTML form, i'm using some loop in ik and nn to show each values from database to tag with <label>,actually i've an error in line 41 and 42, line 41 is $stmt->bindParam(2, $this->ik); Commented May 21, 2017 at 5:54
  • I've made an example answer below, give that a try. If it works please accept, if not please enable error reporting (I didn't test), and/or comment below. Commented May 22, 2017 at 14:12

1 Answer 1

0

Since nn and ik are arrays you need to loop over them and bind each value. Here's a rough example of how you could do that assuming nn and ik have a one to one relationship.

$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);

This is a rough, untested sample of how it should work.

Don't put variables into $query_bits or you will loose the purpose of preparing/binding.

This approach is making on large insert statement with each row's data separated by a comma.

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.

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

Sign up to request clarification or add additional context in comments.

17 Comments

i don't know about how $query_bits working? also on line 1 you forgot to add ; at the end of line,
$query_bits is an array of the data each row will contain. This is one large insert now. ( I figured there'd be a typo somewhere, corrected). I've added the mysql manual reference as well.
when i tried your code above, i've got an error syntax error, unexpected '$query_bits' (T_VARIABLE) on line 8 that's mean i need to create a new variable called $query_bits first?
That sounds like it didn't enter the foreach. Is $this->ik populated?
yes i though, because i can use my function last time for <select>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.