Simpan data ke database dari data berupa checkbox

Dwi Apriyantono · Dec 18, 2014
gimana ya cara simpan data nya yang diquery dari satu tabel, kemudian datanya dipilih dengan checkbox kemudian disimpan ke tabel lainnya?

saya coba buat seperti ini :


<form action="prosestransfer.php" methode="post">
        <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered datatables" id="example">
            <thead>
                <tr>
                    <th width="5%"><span><input type="checkbox" id="select-all"></span></th>
                    <th width="250px">Nama Pelanggan</th>
                    <th>Kota</th>
                    <th>Nomor faktur</th>
                    <th>Total nominal</th>
                </tr>
            </thead>

            <tbody class="selects">
                <?php
                $qry = "select a.* from mst_inv a order by a.inv_id;";
                $hasil = mysql_query($qry);
                $jumlah=$count;
                while ($data = mysql_fetch_array($hasil)) {
                    $inv_id = [0];
                    $inv_cust_code = [1];
                    $inv_cust_name = [2];
                    $inv_cust_address = [3];
                    $inv_number = [4];
                    $inv_receivable = [5];

                    echo "<tr class=\"odd gradeX\">
                    <td>
                        <input type=\"checkbox\" name=\"item\" id=\"item-$inv_id value=\"$inv_id\" >
                        <input type=\"hidden\" name=\"inv_cust_code[$inv_id]\" value=\"$inv_cust_code\" >
                        <input type=\"hidden\" name=\"inv_cust_name[$inv_id]\" value=\"$inv_cust_name\" >
                        <input type=\"hidden\" name=\"inv_cust_address[$inv_id]\" value=\"$inv_cust_address\" >
                        <input type=\"hidden\" name=\"inv_number[$inv_id]\" value=\"$inv_number\" >
                        <input type=\"hidden\" name=\"inv_receivable[$inv_id]\" value=\"$inv_receivable\" >
                            
                    </td>
                    <td> $data[inv_cust_name] </td>
                    <td> $data[inv_cust_city] </td>
                    <td> $data[inv_number]</td>
                    <td> $data[inv_value]</td>

                </tr>";
                }
                ?>
            </tbody>

        </table>

        <div class="panel-footer">
            <div class="pull-right">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-success">Save changes</button>
                <input type="hidden" id="aksi" value="transferfaktur"
            </div>
        </div>
</div>
</form>
<?php
include_once 'conf/db.php';
 
session_start();
$aksi = (isset($_POST["aksi"])) ? $_POST["aksi"] : null;
 
 
//============= begin action for user =======================
if ($aksi == 'transferfaktur') {
 
        foreach($_POST['item']as $inv_id){
            //$insert=mysql_query("INSERT INTO temp_inv (inv_id, inv_cust_code, inv_cust_name, inv_cust_address, inv_number, inv_receivable) VALUES ('$inv_id', '$inv_cust_code', '$inv_cust_name', '$inv_cust_address','$inv_number','$inv_receivable')")or die(mysql_error());
            $insert=mysql_query("INSERT INTO temp_inv (inv_id, inv_cust_code, inv_cust_name, inv_cust_address, inv_number, inv_receivable) VALUES ('$inv_id', '".$_POST['inv_cust_code']['$inv_id']."','".$_POST['inv_cust_name']['$inv_id']."','".$_POST['inv_cust_address']['$inv_id']."','".$_POST['inv_number']['$inv_id']."','".$_POST['inv_receivable']['$inv_id']."')")
                    or die(mysql_error());
           
            if (!insert){
                die('Error');
            }else{
                echo $insert;
            }
        }
   
}
 
?>

hasilnya nihil, data ndak ada yg masuk ke db dan kalau di echo tidak muncul apa2... hanya di address bar browser muncul panjang :
prosestransfer.php?example_length=10&inv_cust_code%5BArray%5D=Array&inv_cust_name%5BArray%5D=Array&inv_cust_address%5BArray%5D=Array&inv_number%5BArray%5D=Array&inv_receivable%5BArray%5D=Array&inv_cust_code%5BArray%5D=Array&inv_cust_name%5BArray%5D=Array&inv_cust_address%5BArray%5D=Array&inv_number%5BArray%5D=Array&inv_receivable%5BArray%5D=Array&inv_cust_code%5BArray%5D=Array&inv_cust_name%5BArray%5D=Array&inv_cust_address%5BArray%5D=Array&inv_number%5BArray%5D=Array&inv_receivable%5BArray%5D=Array&item=on&inv_cust_code%5BArray%5D=Array&inv_cust_name%5BArray%5D=Array&inv_cust_address%5BArray%5D=Array&inv_number%5BArray%5D=Array&inv_receivable%5BArray%5D=Array&inv_cust_code%5BArray%5D=Array&inv_cust_name%5BArray%5D=Array&inv_cust_address%5BArray%5D=Array&inv_number%5BArray%5D=Array&inv_receivable%5BArray%5D=Array
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Dec 30, 2014 · 0 Suka · 0 Tidak Suka
pertama formnya salah:
<form action="prosestransfer.php" methode="post">
seharusnya
<form action="prosestransfer.php" method="post">
Kemudian nama checkbox:
<input type=\"checkbox\" name=\"item\" id=\"item-$inv_id value=\"$inv_id\" >
seharusnya
<input type=\"checkbox\" name=\"item[]\" id=\"item-$inv_id value=\"$inv_id\" >
<?php
include_once 'conf/db.php';

session_start();
$aksi = (isset($_POST["aksi"])) ? $_POST["aksi"] : null;


//============= begin action for user =======================
if ($aksi == 'transferfaktur') {

	foreach($_POST['item']as $inv_id){
		//$insert=mysql_query("INSERT INTO temp_inv (inv_id, inv_cust_code, inv_cust_name, inv_cust_address, inv_number, inv_receivable) VALUES ('$inv_id', '$inv_cust_code', '$inv_cust_name', '$inv_cust_address','$inv_number','$inv_receivable')")or die(mysql_error());
		$insert=mysql_query("INSERT INTO temp_inv (inv_id, inv_cust_code, inv_cust_name, inv_cust_address, inv_number, inv_receivable) VALUES ('$inv_id', '".$_POST['inv_cust_code'][$inv_id]."','".$_POST['inv_cust_name'][$inv_id]."','".$_POST['inv_cust_address'][$inv_id]."','".$_POST['inv_number'][$inv_id]."','".$_POST['inv_receivable'][$inv_id]."')")
		or die(mysql_error());
		
		if (!insert){
		die('Error');
		}else{
		echo $insert;
		}
	}

}

?>