【问题标题】:Insert multiple row from php page into mysql将多行从php页面插入mysql
【发布时间】:2018-04-19 09:35:20
【问题描述】:

从昨天开始,这个问题就把我逼疯了。我有一个包含 5 列的表:kode_barang(项目 ID)、nama_barang(项目名称)、qty(数量)、harga_beli(价格)、jumlah(总计)。用户可以输入 2 个项目。这是表单的代码:

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

这是与我的数据库的连接:

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

请注意,我的“Kode Barang”是一个下拉选项,每次用户单击项目 ID 时,项目的名称都会自动显示在“Nama Barang”列中。此页面中的所有内容都运行良好。

但是,当我将它保存到数据库时,它并没有保存两个项目(第一行和第二行中的项目)。数据库只保存了第二项,但保存了两次。当我向表中添加一行变为 3 行时,数据库只保存第三项并保存三次。当我在名称中添加[] 时,例如name=kode_barang[],数据库没有保存项目ID,而只显示“数组”文本。

有人可以帮我解决这个问题吗?谢谢。

【问题讨论】:

  • 警告:如果您只是学习 PHP,请不要使用mysql_query 接口。它是如此可怕和危险,以至于它在 PHP 7 中被删除。像 PDO is not hard to learn 这样的替代品和像 PHP The Right Way 这样的指南解释了最佳实践。您的用户数据不是properly escaped,有SQL injection bugs,可以被利用。
  • &lt;td type="text" name="kode_barang" id="kode_barang1"/readonly&gt; 据我所知,&lt;td&gt; 没有类型或名称。

标签: php mysql


【解决方案1】:

您的两个字段使用相同的名称 使用数组 name="nama_barang[]"and name="kode_barang[]"

在php中做一个循环来单独保存每个字段

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2014-02-25
    • 2012-01-04
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多