使用动态表格插入数据库

时间:2018-11-12 07:19:08

标签: php

我试图从动态表单中将数据插入数据库中,成功地插入了数据,但是只有最新的输入字段将被添加到数据库中(示例:我使用按钮向表单中添加了新输入在网络中,要插入的数据只是新输入,而没有插入新输入之前的输入)。

<?php 
include 'config/db_connect.php';

if(isset($_POST['submit']))
{
     $i=0;
     foreach($_POST['kp_berjangka'] as $value){
    // insert query values can be fetched by
$file = $_POST['file'];
$selectidmax =mysqli_query($con, "SELECT max(id_kebijakan) as maxidadmin FROM t_kebijakan WHERE id_kebijakan LIKE 'KO%'");
$hslidmax=mysqli_fetch_array($selectidmax);
$idmax=$hslidmax['maxidadmin'];
$nourut = (int) substr($idmax, 2,3);
$nourut++;
$IDbaru = "KO" . sprintf("%03s", $nourut);
$kp_saham = $_POST['kp_saham'][$i];
$pro_saham = $_POST['produk_saham'][$i];
$bung_saham = $_POST['bunga_saham'][$i];
$ket_saham = $_POST['ket_saham'][$i];
$kp_harian = $_POST['kp_harian'][$i];
$pro_harian = $_POST['produk_harian'][$i];
$bung_harian = $_POST['bunga_harian'][$i];
$ket_harian = $_POST['ket_harian'][$i];
$kp_berjangka = $_POST['kp_berjangka'][$i];
$pro_berjangka = $_POST['produk_berjangka'][$i];
$bung_berjangka = $_POST['bunga_berjangka'][$i];
$ket_berjangka = $_POST['ket_berjangka'][$i];
$kp_deposito = $_POST['kp_deposito'][$i];
$pro_deposito = $_POST['produk_deposito'][$i];
$bung_deposito = $_POST['bunga_deposito'][$i];
$ket_deposito = $_POST['ket_deposito'][$i];
$kp_pinjaman = $_POST['kp_pinjaman'][$i];
$pro_pinjaman = $_POST['produk_pinjaman'][$i];
$bung_pinjaman = $_POST['bunga_pinjaman'][$i];
$ket_pinjaman = $_POST['ket_pinjaman'][$i];
$id = $_POST['meja'][$i];

$query = mysqli_query($con,"INSERT INTO t_kebijakan (id_kebijakan, file, kp_saham, produk_saham, bunga_saham, keterangan_saham, kp_harian, produk_harian, bunga_harian, keterangan_harian, kp_berjangka, produk_berjangka, bunga_berjangka, keterangan_berjangka, kp_deposito, produk_deposito, bunga_deposito, keterangan_deposito, kp_pinjaman, produk_pinjaman, bunga_pinjaman, keterangan_pinjaman, kode_cu) 
    VALUES (
    '$IDbaru',
    '$file',
    '$kp_saham',
    '$pro_saham',
    '$bung_saham',
    '$ket_saham',
    '$kp_harian',
    '$pro_harian',
    '$bung_harian',
    '$ket_harian',
    '$kp_berjangka',
    '$pro_berjangka',
    '$bung_berjangka',
    '$ket_berjangka',
    '$kp_deposito',
    '$pro_deposito',
    '$bung_deposito',
    '$ket_deposito',
    '$kp_pinjaman',
    '$pro_pinjaman',
    '$bung_pinjaman',
    '$ket_pinjaman',
    '$id'
    )");
    }


if($query){
    header('location: home_cu.php');
}else{
    echo "data gagal dimasukan" . mysqli_error($con) . mysqli_errno($con);
}

}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Add More Input Form</title>

    <!-- Bootstrap -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <?php 
    include 'config/db_connect.php';
    $kode_cu = $_GET['kode_cu'];
    $query_mysql = mysqli_query($con,"SELECT * FROM t_cu WHERE kode_cu='$kode_cu'")or die(mysqli_error());
    $nomor = 1;
    while($data = mysqli_fetch_array($query_mysql)){
    ?>
    <div class="container">
        <h1 class="text-center">Tambah Data Kebijakan</h1>
        <form action="add_kebijakan.php" method="post">
        <h1 class="text-left">File</h1>
        <div class="form-content">
        <label>File :</label>
        <input type="file" name="file">
        <input type="hidden" value="<?php echo $data['kode_cu']?>" name="meja[]" class="form-control"/>
        </div>
                <h1 class="text-center">Produk Simpanan Saham</h1>
                <div class="form-content">
                <div class="row">
                    <div class="col-md-12">
                        <p><button type="button" id="btnAdd" class="btn btn-primary">Add More</button></p>
                        <br/>
                    </div>
                </div>
                <div class="row group">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Kode Produk :</label>
                            <input type="text" name="kp_saham[]" class="form-control">
                        </div>
                    </div>
                <?php } ?>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Produk</label>
                            <input type="text" name="produk_saham[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Bunga</label>
                            <input type="text" name="bunga_saham[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-5">
                        <div class="form-group">
                            <label>Keterangan</label>
                            <textarea name="ket_saham[]" class="form-control" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <button type="button" class="btn btn-danger btnRemove">Remove</button>
                        </div>
                    </div>
                </div>
            </div>
            <h1 class="text-center">Produk Simpanan Harian</h1>
                <div class="form-content">
                <div class="row">
                    <div class="col-md-12">
                        <p><button type="button" id="btnAdd" class="btn btn-primary">Add More</button></p>
                        <br/>
                    </div>
                </div>
                <div class="row group">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Kode Produk :</label>
                            <input type="text" name="kp_harian[]" class="form-control"/>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Produk</label>
                            <input type="text" name="produk_harian[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Bunga</label>
                            <input type="text" name="bunga_harian[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-5">
                        <div class="form-group">
                            <label>Keterangan</label>
                            <textarea name="ket_harian[]" class="form-control" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <button type="button" class="btn btn-danger btnRemove">Remove</button>
                        </div>
                    </div>
                </div>
            </div>
            <h1 class="text-center">Produk Simpanan Berjangka</h1>
                <div class="form-content">
                <div class="row">
                    <div class="col-md-12">
                        <p><button type="button" id="btnAdd" class="btn btn-primary">Add More</button></p>
                        <br/>
                    </div>
                </div>
                <div class="row group">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Kode Produk :</label>
                            <input type="text" name="kp_berjangka[]" class="form-control"/>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Produk</label>
                            <input type="text" name="produk_berjangka[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Bunga</label>
                            <input type="text" name="bunga_berjangka[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-5">
                        <div class="form-group">
                            <label>Keterangan</label>
                            <textarea name="ket_berjangka[]" class="form-control" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <button type="button" class="btn btn-danger btnRemove">Remove</button>
                        </div>
                    </div>
                </div>
            </div>
            <h1 class="text-center">Produk Simpanan Deposito</h1>
                <div class="form-content">
                <div class="row">
                    <div class="col-md-12">
                        <p><button type="button" id="btnAdd" class="btn btn-primary">Add More</button></p>
                        <br/>
                    </div>
                </div>
                <div class="row group">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Kode Produk :</label>
                            <input type="text" name="kp_deposito[]" class="form-control"/>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Produk</label>
                            <input type="text" name="produk_deposito[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Bunga</label>
                            <input type="text" name="bunga_deposito[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-5">
                        <div class="form-group">
                            <label>Keterangan</label>
                            <textarea name="ket_deposito[]" class="form-control" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <button type="button" class="btn btn-danger btnRemove">Remove</button>
                        </div>
                    </div>
                </div>
            </div>
            <h1 class="text-center">Produk Pinjaman</h1>
                <div class="form-content">
                <div class="row">
                    <div class="col-md-12">
                        <p><button type="button" id="btnAdd" class="btn btn-primary">Add More</button></p>
                        <br/>
                    </div>
                </div>
                <div class="row group">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Kode Produk :</label>
                            <input type="text" name="kp_pinjaman[]" class="form-control"/>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Produk</label>
                            <input type="text" name="produk_pinjaman[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Bunga</label>
                            <input type="text" name="bunga_pinjaman[]" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-5">
                        <div class="form-group">
                            <label>Keterangan</label>
                            <textarea name="ket_pinjaman[]" class="form-control" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <button type="button" class="btn btn-danger btnRemove">Remove</button>
                        </div>
                    </div>
                </div>
            </div>
            <p>
            <button type="submit" class="btn btn-success" name="submit">Simpan</button>
            </p><br/>
        </form>
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="assets/js/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="jquery.multifield.min.js"></script>
    <script>
        $('.form-content').multifield({
            section: '.group',
            btnAdd:'#btnAdd',
            btnRemove:'.btnRemove',
        });
    </script>
  </body>
</html>

这是我添加echo "<pre>";print_r($_POST['kp_saham']);

后的结果
Array
(
    [0] => ASD
)
Array
(
    [0] => ADS
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ADS
)
Array
(
    [0] => ADS
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ADS
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => AD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ASD
)
Array
(




[0] => ASD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => ASD
)
Array
(
    [0] => CO001
)

注意:ASD,ADS只是随机输入的单词

2 个答案:

答案 0 :(得分:2)

在这部分或其他部分中,尝试在输入的[]中添加name。例子

                <div class="row group">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Kode Produk :</label>
                            <input type="text" name="kp_berjangka[]" class="form-control"/>
                        </div>
                    </div>

然后在表单操作中执行此操作

echo "<pre>";print_r($_POST['kp_saham']);

答案 1 :(得分:1)

据我了解,您已经多次使用jquery multifield js生成某些表单字段。要保存多个记录,您必须执行以下步骤。

1。通过在输入控件的名称中添加[]来创建控件数组,即

<input type="text" name="kp_berjangka[]" class="form-control"/>

这需要使用所有需要存储在数据库中的输入字段完成

2。在处理发布请求时,您需要使用循环保存数据。代码将类似这样。 即

 $i=0;
 foreach($_POST['kp_berjangka'] as $value){
    // insert query values can be fetched by
    $IDbaru = "KO" . sprintf("%03s", $nourut);
    $kp_saham = $_POST['kp_saham'][$i];
    $pro_saham = $_POST['produk_saham'][$i];
    $bung_saham = $_POST['bunga_saham'][$i];
    $ket_saham = $_POST['ket_saham'][$i];
    $kp_harian = $_POST['kp_harian'][$i];
    $pro_harian = $_POST['produk_harian'][$i];
    $bung_harian = $_POST['bunga_harian'][$i];
    $ket_harian = $_POST['ket_harian'][$i];
    $kp_berjangka = $_POST['kp_berjangka'][$i];
    $pro_berjangka = $_POST['produk_berjangka'][$i];
    $bung_berjangka = $_POST['bunga_berjangka'][$i];
    $ket_berjangka = $_POST['ket_berjangka'][$i];
    $kp_deposito = $_POST['kp_deposito'][$i];
    $pro_deposito = $_POST['produk_deposito'][$i];
    $bung_deposito = $_POST['bunga_deposito'][$i];
    $ket_deposito = $_POST['ket_deposito'][$i];
    $kp_pinjaman = $_POST['kp_pinjaman'][$i];
    $pro_pinjaman = $_POST['produk_pinjaman'][$i];
    $bung_pinjaman = $_POST['bunga_pinjaman'][$i];
    $ket_pinjaman = $_POST['ket_pinjaman'][$i];
    $id = $_POST['meja'][$i];

    $query = mysqli_query($con,"INSERT INTO t_kebijakan (id_kebijakan, file, kp_saham, produk_saham, bunga_saham, keterangan_saham, kp_harian, produk_harian, bunga_harian, keterangan_harian, kp_berjangka, produk_berjangka, bunga_berjangka, keterangan_berjangka, kp_deposito, produk_deposito, bunga_deposito, keterangan_deposito, kp_pinjaman, produk_pinjaman, bunga_pinjaman, keterangan_pinjaman, kode_cu) 
        VALUES (
        '$IDbaru',
        '$file',
        '$kp_saham',
        '$pro_saham',
        '$bung_saham',
        '$ket_saham',
        '$kp_harian',
        '$pro_harian',
        '$bung_harian',
        '$ket_harian',
        '$kp_berjangka',
        '$pro_berjangka',
        '$bung_berjangka',
        '$ket_berjangka',
        '$kp_deposito',
        '$pro_deposito',
        '$bung_deposito',
        '$ket_deposito',
        '$kp_pinjaman',
        '$pro_pinjaman',
        '$bung_pinjaman',
        '$ket_pinjaman',
        '$id'
    )");
}

通过此循环,您将获得表单提交的所有记录。

3。您无法使用$_POST['FILE']保存文件,请检查https://www.w3schools.com/php/php_file_upload.asp中的文件上传过程,这将为您提供帮助