This problem's driving me crazy since yesterday. I have a table consist of 5 columns: kode_barang (item ID), nama_barang (name of item), qty (quantity), harga_beli (price), jumlah (total). User can input 2 items. This is the code of the form:

<HTML> <?php include "koneksi.php"; ?> 
<form action="insert3.php" method="POST"> 

<table id="theTable" border="1"> 
<thead> 
   <tr>
      <th> Kode Barang</th>
      <th> Nama Barang </th> 
      <th> Qty </th> 
      <th> Harga Beli </th> 
      <th> Jumlah </th> 
   <tr>
</thead> 

<tbody> 
   <tr> 
      <td type="text" name="kode_barang" id="kode_barang1"/readonly> 
         <?php  
                mysql_connect("localhost","root","");  
                mysql_select_db("skripsi_1");  
                $result = mysql_query("select * from input_data_barang");  
                $met = "var kode_barang = new Array();\n";  
                echo '<select name="kode_barang" onchange="changeValue1(this.value)">';  
                echo '<option></option>';  
                while ($row = mysql_fetch_array($result)) {  
                echo '<option value="' . $row['kode_barang'] . '">' . $row['kode_barang'] . '</option>';  
                $met .= "kode_barang['" . $row['kode_barang'] . "'] = {name:'" . addslashes($row['nama_barang']) . "',desc:'".addslashes($row['nama_barang'])."'};\n";  
                } 
                echo '</select>';  
         ?> 

     </td> <td><input type="text" name="nama_barang" id="nama_barang1"/readonly> 
        <script type="text/javascript"> <?php echo $met; ?>
                function changeValue1(id){
                document.getElementById('kode_barang1').value = kode_barang[id].name;
                document.getElementById('nama_barang1').value = kode_barang[id].desc;
                };
        </script>
     </td> 

     <td><input class="valOne" type="text" name="qty"></td> 
     <td><input class="valTwo" type="text" name="harga_beli"></td> 
     <td><input class="result" type="text" name="jumlah"></td> 
  </tr> 

  <tr> 
     <td type="text" name="kode_barang" id="kode_barang2"/readonly> 
        <?php  
                mysql_connect("localhost","root","");  
                mysql_select_db("skripsi_1");  
                $result = mysql_query("select * from input_data_barang");  
                $jsArray = "var kode_barang = new Array();\n";  
                echo '<select name="kode_barang" onchange="changeValue2(this.value)">';  
                echo '<option></option>';  
                while ($row = mysql_fetch_array($result)) {  
                echo '<option value="' . $row['kode_barang'] . '">' . $row['kode_barang'] . '</option>';  
                $jsArray .= "kode_barang['" . $row['kode_barang'] . "'] = {name:'" . addslashes($row['nama_barang']) . "',desc:'".addslashes($row['nama_barang'])."'};\n";  
                } 
                echo '</select>';  
         ?> 
      </td> 

      <td><input type="text" name="nama_barang" id="nama_barang2"/readonly> 
         <script type="text/javascript"> <?php echo $jsArray; ?>
               function changeValue2(id){
               document.getElementById('kode_barang2').value = kode_barang[id].name;
               document.getElementById('nama_barang2').value = kode_barang[id].desc;
               };
         </script> 

      </td> <td><input class="valOne" type="text" name="qty"></td> 
      <td><input class="valTwo" type="text" name="harga_beli"></td> 
      <td><input class="result" type="text" name="jumlah"></td> 
   </tr> 

  <script>
        document.getElementById("theTable").addEventListener("input", function(e) {
        var row = e.target.parentNode.parentNode
        var val1 = row.querySelector(".valOne").value
        var val2 = row.querySelector(".valTwo").value
        row.querySelector(".result").value = val1 * val2
        })
   </script> 

  </tbody> 

  <td><input type="submit" value="OK"></a> 
  <input type="reset" value="Reset"></td> 

  </table> 
  </form> 
  </HTML>

And this is the connection to my database (insert3.php):

<?php
include "koneksi.php";

$kode_barang=$_POST['kode_barang'];
$nama_barang=$_POST['nama_barang'];
$qty=$_POST['qty'];
$harga_beli=$_POST['harga_beli'];
$jumlah=$_POST['jumlah'];

$query ="INSERT INTO faktur (kode_barang, nama_barang, qty, harga_beli, jumlah) VALUES ('".$kode_barang."', '".$nama_barang."', '".$qty."', '".$harga_beli."', '".$jumlah."'), ('".$kode_barang."', '".$nama_barang."', '".$qty."', '".$harga_beli."', '".$jumlah."')"; 
$sql =mysqli_query($connect, $query);

if ($sql){
header ("location: faktur.php");
}else{
echo "Error.";
        echo "<br><a href='input_faktur.php'>Back</a>";
}
?>

Notice that my 'Kode Barang' is a dropdown option, and everytime a user click an item ID, name of the item will be shown automatically in 'Nama Barang' column. Everything in this page works perfectly.

But, when I saved it to database, it didn't save both of the items (item in the first row and the second row). Database only saved the second item but saved it twice. When I add a row to the table become 3 rows, database only save the third item and save it three times.

I've tried to add [] to the name, like name=kode_barang[] and name=nama_barang[], but database didn't save the item ID, but only show "Array" text in mysql.

Would anybody please help me with this? Thanks.

Let's set your "problem" aside for a moment.

You are using dangerous obsolete mysql code that has been completetly removed from Php and will not work in the current versions at all no matter what you do. You need to use Prepared Statements. I reccomend you use PDO. https://phpdelusions.net/pdo.

That is enough wrong alone that there is really no point detailing anything else about it except to say NEVER EVER put variables in a query, do not create variables for nothing and do not SELECT *. Specify the column names you want explicitly.

Re-write your code in PDO after you study it and come back if you are still having problems. I will be happy to help once you do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.