请帮助我解决以下问题

时间:2019-07-17 07:02:49

标签: php

我有下面的代码,我想问一下如何根据源数据将源插入到其他表中的数据库中?

我从数据库中获取数据并将其显示在复选框上,但是当我选中该复选框并按Tambah按钮时,数据将插入到表中,但数据错误,只是0和数组。

<!DOCTYPE html>
<html>
<head>
	<title>kumpulan data command</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<h1>Evaluation</h1>
	<h2>Data command</h2>
	<?php

$db = new mysqli("localhost","root","","FWS_online");
echo $db->connect_errno?'Koneksi gagal :'.$db->connect_error:'';
$query = ("SELECT * FROM printer_function_commands") 	or die(mysql_error());
$result = mysqli_query($db, $query);
$pilihan = '';
$data = array();
if (isset($_POST['submit']) && isset($_POST['commands']))
{
    if (count($_POST['commands']) > 0)
    {
        $pilihan = serialize($_POST['commands']);
        echo "<p>Data berhasil disimpan ke database berupa ".$pilihan."</p>";
    }
}
if ($pilihan <> '')
{
    $data = unserialize($pilihan);
}
?>

	<table border="1">
		<tr>
			<th>ID</th>
			<th>No</th>
			<th>Commands</th>
		</tr>
		<?php 
		include "database.php";
		$data1 = mysql_query("select * from result_commands");
		$no = 1;
		while($d = mysql_fetch_array($data1)){
		?>
		<tr>
			<td><?php echo $no++; ?></td>
				<td><?php echo $d['no_commands']; ?></td>
				<td><?php echo $d['commands']; ?></td>		
		</tr>
		<?php } ?>
	</table>
	<br/>
	<h2>Input Banyak Data</h2>
	
<form method="post" action="tambah.php">		
	<table border="1">
			<tr>
				<th>ID</th>
				<th>No</th>
				<th>Commands</th>
				<th>Pilih</th>
			</tr>
			<?php 
			include "database.php";
			$data = mysql_query("select * from printer_function_commands");
			$no = 1;
			while($d = mysql_fetch_array($data)){
			?>
			<tr>
				<td><?php echo $no++; ?></td>
				<td><?php echo $d['no_commands']; ?></td>
				<td><?php echo $d['commands']; ?></td>	
				<td><input type="checkbox" name="pilih[]" value="<?php echo $d['id_commands']; ?>"></td>	
			</tr>
			<?php } ?>
		</table>
		<input type="submit" name="insert" value="Tambah">
	</form>

</body>
</html>

以下添加到数据库的代码。

<?php 
include 'database.php';
$id_commands = $_POST['pilih'];
$no_commands = $_POST['pilih'];
$jumlah_dipilih = count($no_commands);

for($x=0;$x<$jumlah_dipilih;$x++){
	mysql_query("INSERT INTO result_commands VALUES(NULL,'$id_commands', '$no_commands')") or die(mysql_error());
}

header("location:index.php");
?>

在下面附上结果。 enter image description here

1 个答案:

答案 0 :(得分:1)

1-您要插入数组而不是值。

2- no_commands值还将添加到输入隐藏字段中,以在提交表单后获取该值。

  $no_commands = $_POST['no_commands'];  //In table add this value in input hidden field to get the value after submit

    $id_commands = $_POST['pilih'];

    $jumlah_dipilih = count($id_commands); 
    /*Before iterate the array value check what data you are getting for 
      id_commands using print_r 
      like,  echo "<pre>"; print_r($id_commands); echo "</pre>"; 
     */
    for($x=0;$x<$jumlah_dipilih;$x++){ 
        $id_commands_val = $id_commands[$x]; //insert the id_commands value instead of array
        mysqli_query("INSERT INTO result_commands VALUES(NULL,'$id_commands_val', '$no_commands')");
    } 

根据注释更新代码,可能会有所帮助。

<form method="post" action="tambah.php">    
    <table border="1">
            <tr>
                <th>ID</th>
                <th>No</th>
                <th>Commands</th>
                <th>Pilih</th>
            </tr>
            <?php 
            $data = mysql_query("select * from printer_function_commands");
            $no = 1;
            while($d = mysql_fetch_array($data)){

            ?>
            <!-- no_commands value in hidden field -->
            <input type="hidden" name="pilih[<?php echo $no;?>][no_commands]" value="<?php echo $d['no_commands']; ?>">
            <tr>
                <td><?php echo $no; ?></td>
                <td><?php echo $d['no_commands']; ?></td>   
                <td><?php echo $d['commands']; ?></td>
                <td><input type="checkbox" name="pilih[<?php echo $no;?>][id]" value="<?php echo $d['id_commands']; ?>"></td>   
            </tr>
            <?php 
            $no++;
            } ?>
        </table>
        <input type="submit" name="insert" value="Tambah">
    </form>

tambah.php

<?php 
include 'database.php';
if(isset($_POST['insert'])){
    $id_commands = $_POST['pilih'];

    /*echo "<pre>";
    print_r($id_commands);*/

    $id_commands_length = count($id_commands);
    for($i=1; $i<=$id_commands_length; $i++){

        if(isset($id_commands[$i]['id']) && $id_commands[$i]['id'] != ''){
            echo $id_commands_val = $id_commands[$i]['id'];
            echo $id_no_val = $id_commands[$i]['no_commands'];
            mysql_query("INSERT INTO result_commands VALUES(NULL,'$id_commands_val', '$id_no_val')") or die(mysql_error());
        }

    }

}
?>
相关问题