0

I have a form that looks like:

|-----------------------|
|Text                   |
|(dropdown)             |
------------------------|
|label (value = 1)      | //get this by loop using while
|input                  | //get this by loop using while
|-----------------------|
|label (value = 2)      | //get this by loop using while
|input                  | //get this by loop using while
|-----------------------|
|label (value = ...)    | //get this by loop using while
|input                  | //get this by loop using while
|-----------------------|
|submit                 |

I dunno why my form just insert last value

example

  1. choose dropdown value = 1
  2. add value of input = 80 on label 1
  3. add value of input = 79 on label 2

FYI

  1. [ia] = dropdown value
  2. [ik] = label value
  3. [nn] = input value

when I click the submit button with print_r($_POST); the output is Array ( [ia] => 1 [ik] => 2 [nn] => 79 )

I want to get

Array ( [ia] => 1 [ik] => 1 [nn] => 80 )
Array ( [ia] => 1 [ik] => 2 [nn] => 79 )

this is my code about using while to display another form:

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->insert()){
?>
<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
        $no=1;
        $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>

rangking.inc.php

function insert(){

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

    }
dreq
  • 39
  • 7

1 Answers1

0

You have multiple elements with the same name, if you post that form it will only send the values of the last element.

You should change your these two lines:

<input type="hidden" name="ik" id="ik" value=<?php echo $id_kriteria ?>>
<input type="text" class="form-control" id="nn" name="nn">

to this:

<input type="hidden" name="ik[]" id="ik" value=<?php echo $id_kriteria ?>>
<input type="text" class="form-control" id="nn" name="nn[]">

That way it is passed as an array which should contain every input. You can check this answer for more information.

Community
  • 1
  • 1
Tom Udding
  • 2,264
  • 3
  • 20
  • 30
  • this code help me out about an array that i want, but why the error showing `Array to string conversion`, should i edit `rangking.inc.php` code? – dreq May 19 '17 at 16:50
  • Well you try to use the actual array instead of the values in the array. You are trying to insert the actual array into your database. You'll have to loop the insert function (inside or outside) and pass one row of the array at the time. You can check [this answer](https://stackoverflow.com/a/14167510/5914775) for more information. – Tom Udding May 19 '17 at 16:51
  • thanks for every suggest, it's time to 'fight' with an array, – dreq May 19 '17 at 16:59