【问题标题】:Cannot insert multiple value from database value form php无法从数据库值表单php中插入多个值
【发布时间】:2016-05-08 06:33:48
【问题描述】:

这是我得到的错误:

注意:未定义索引:C:\xampp\htdocs\PTG 中的 no_siri 第 4 行的 Sistem\penyata.php 注意:未定义索引:第 6 行 C:\xampp\htdocs\PTG Sistem\penyata.php 中的 agihan

INSERT INTO penyata (no_siri, tarikh, penerangan, jumlah_bayaran)
     VALUES
     ('','2016-01-30 21:57:08','Agihan keuntungan adalah sebanyak %','720')

注意:未定义索引:C:\xampp\htdocs\PTG Sistem\penyata.php 中的 1 第 23 行


表格:

 <form method="get" action="penyata.php">
      <div class="panel-body">
      <table class="table table-striped table-hover">
                    <thead>
                    <tr>

                        <th></th>
                        <th>No Siri</th>
                        <th>Tarikh</th>
                        <th>Nama</th>
                        <th>Bank</th>
                        <th>Akaun Bank</th>
                        <th>Lot</th>
                        <th>Agihan</th>
                        <th>Bayaran</th>

                    </tr>
                    </thead>
                    <tbody>';


                        while($rows_client = mysql_fetch_array($bayar_client)){

                            $total_bayar = $rows_client['lot'] * ($bayaran /100);

               echo '<tr>

                        <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$i++.'" /required></td>
                        <td><input name="no_siri[]" type="text" id="no_siri[]" value="'.$rows_client['no_siri'].'" /disabled></td>
                        <td>'.$rows_client['hari'].'/'.$rows_client['bulan'].'/'.$rows_client['tahun'].'</td>
                        <td>'.$rows_client['nama'].'</td>
                        <td>'.$rows_client['nama_bank'].'</td>
                        <td>'.$rows_client['akaun_bank'].'</td>
                        <td>RM'.$rows_client['lot'].'</td>
                        <td><input name="agihan[]" type="text" id="agihan[]" value="'.$bayaran.'" /disabled>%</td>
                        <td>RM <input name="jumlah_bayaran[]" type="text" id="jumlah_bayaran[]" value="'.$total_bayar.'"></td>

                    </tr>';
                        }
                echo '
                        <button type="submit" class="btn btn-info" name="simpan">
                            <i class="fa fa-database"></i>
                        </button>
                        &nbsp;
                        <button class="btn btn-info" onClick="myFunction()" title="Cetak">
                            <i class="fa fa-print"></i>
                        </button>

                        <script>
                            function myFunction() {
                                    window.print();
                            }
                        </script>



                    </tbody>
                    </form>

PHP:

include("dbconn.php");

$no_siri = $_GET['no_siri'];
$agihan = $_GET['agihan'];
date_default_timezone_set('Asia/Kuala_Lumpur');
$tarikh[] = date('Y-m-d H:i:s', time());
$penerangan[] = "Agihan keuntungan adalah sebanyak ".$agihan."%";
$jumlah_bayaran = $_GET['jumlah_bayaran'];

$add_penyata = "INSERT INTO penyata (no_siri, tarikh, penerangan, jumlah_bayaran) VALUES";

$query_parts = array();
foreach($_GET['checkbox'] as $i){
    //for($x=0; $x<count($i); $x++){
        $query_parts[] = "('{$no_siri[$i]}','{$tarikh[$i]}','{$penerangan[$i]}','{$jumlah_bayaran[$i]}')";
    //}
    echo $add_penyata .= implode(',', $query_parts);
}               
//$result = mysql_query($add_penyata);

//echo $add_penyata;

if($result){
    echo '<script type="text/javascript">window.alert("Rekod Berjaya Disimpan.") </script>';
    echo "<script language='JavaScript'>window.location ='akaun.php'</script>";
}
else{
    echo '<script type="text/javascript">window.alert("Ralat!!!!.") </script>';
}

我想从表单中插入多个数据,就像

INSERT INTO `penyata`(`id`, `no_siri`, `tarikh`, `penerangan`, `jumlah_bayaran`)
     VALUES
     ([value-1],[value-2],[value-3],[value-4],[value-5]),
     ([value-1],[value-2],[value-3],[value-4],[value-5]),
     and more....

但我不知道如何在 php 上制作它。我非常感谢任何解决方案。非常感谢。

【问题讨论】:

  • 自 PHP 5.5 起不推荐使用 MySQL,您应该改用 MySQLiPDO。也看看Medoo

标签: php mysql


【解决方案1】:

先做:

if(! $_GET['no_siri']) {
  $no_siri = $_GET['no_siri'];
}

首先你必须检查值是否存在。

【讨论】:

  • 我得到这个错误:注意:未定义的索引:C:\xampp\htdocs\PTG Sistem\penyata.php 中的第 4 行中的 no_siri 注意:未定义的索引:C:\xampp\htdocs\ 中的 no_siri PTG Sistem\penyata.php on line 5 INSERT INTO penyata (no_siri, tarikh, penerangan, jumlah_bayaran) VALUES('','2016-01-31 09:37:34','Agihan keuntungan adalah sebanyak %','960' ) 注意:未定义索引:C:\xampp\htdocs\PTG Sistem\penyata.php 第 25 行中的 1 ..
【解决方案2】:

尝试换行:

if(isset($_GET['no_siri'])){
      $no_siri = $_GET['no_siri'];
}

【讨论】:

  • 出现此错误:注意:未定义变量:C:\xampp\htdocs\PTG Sistem\penyata.php 中的 no_siri 第 25 行 INSERT INTO penyata (no_siri, tarikh, penerangan, jumlah_bayaran) VALUES( '','2016-01-31 09:45:42','Agihan keuntungan adalah sebanyak %','960') 注意:未定义变量:C:\xampp\htdocs\PTG Sistem\penyata.php 中的 no_siri 上线25
【解决方案3】:

更短:

$no_siri = @$_GET['no_siri'];

它留下$no_siri 的值或null@ 防止出现错误消息。

【讨论】:

  • 如果它留下了值,那么我应该如何让数据插入到表格中?..例如:INSERT INTO 语句(no_siri,日期,描述,total_payment)VALUES('php1234', '2016-01-31 09:45:42','Description 7%','960'), 那么如果数据多于 1 则应为 INSERT INTO 语句 (no_siri, date, description, total_payment) VALUES('no_siri ','2016-01-31 09:45:42','描述 7%','960'),('php5678','2016-02-01 09:45:42','描述 10%', '1200'),如果显示的数据多,则循环插入更多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 2013-03-24
相关资源
最近更新 更多