将复选框的值保存到数据库中

save the value of checked box into database

这是我的 php 代码。我想将复选框的值保存到数据库中。

$checkbox = "";
if(!empty($_POST)) { 
for (!$i=0; $i<sizeof($checkbox);$i++) {
$query="INSERT INTO qe_qcc (checkbox) VALUES ('".$checkbox[$i]."')";
mysql_query($query) or die(mysql_error());
}

这是我的html代码

<tr>
<td><input type="checkbox" name="check1[]" value="Enhance Productivity" />Enhance      Productivity</td>
<td>     <input type="checkbox" name="check1[]" value="Enhance Quality"   />Enhance Quality</td>
<td>     <input type="checkbox" name="check1[]" value="Save Time" />Save   Time</td>
<td>     <input type="checkbox" name="check1[]" value="Save Materials" />Save Materials</td>
<td>     <input type="checkbox" name="check1[]" value="Save Stock" />Save Stock</td>
<td>     <input type="checkbox" name="check1[]" value="Increase Safety" />Increase Safety</td>
<td>     <input type="checkbox" name="check1[]" value="Financial Saving" />Financial Saving</td>
<td>     <input type="checkbox" name="check1[]" value="Customer Friendly" />Customer Friendly</td>
<td>      <br /><br /><input type="checkbox" name="check1[]" value="Others" />Others</td>
</tr>

试试这个:

   $checkbox=$_POST['check1'];
        for ($i=0; $i<count($checkbox);$i++) {
         $query="INSERT INTO qe_qcc (checkbox) VALUES ('".$checkbox[$i]."')";
           mysql_query($query) or die(mysql_error());

        }

首先不要使用 mysql_ ,它已被弃用..(在 CI .. 你可以很容易地改变它)

其次,如果您使用的是 codeigniter,则使用活动记录

然后是第三...

<?php
if(isset($_POST))
{
     foreach($_POST['check1'] as $rescheck)
     {
         $this->db->insert('qe_qcc', $rescheck); 
     }
 }
 ?>

或者更好地使用

  <?php  $data = array();
 if(isset($_POST))
{
     foreach($_POST['check1'] as $rescheck)
     {
         $data[] = $rescheck
     }
 }
  $this->db->insert_batch('mytable', $data);
 ?>     

要将所有复选框值存储在一列中:

$check_value = implode(',',$_POST['check1']);

你可以使用内爆

$checkbox=implode(',', $_POST['check1']);
$query="INSERT INTO qe_qcc (checkbox) VALUES ('".$checkbox."')";
mysql_query($query) or die(mysql_error());

在HTML

<form name ="check" action="" method="POST">
<tr>
<td><input type="checkbox" name="check1[]" value="Enhance Productivity" />Enhance      Productivity</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="check1[]" value="Enhance Quality"   />Enhance Quality</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="check1[]" value="Save Time" />Save   Time</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="check1[]" value="Save Materials" />Save Materials</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="check1[]" value="Save Stock" />Save Stock</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="check1[]" value="Increase Safety" />Increase Safety</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="check1[]" value="Financial Saving" />Financial Saving</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="check1[]" value="Customer Friendly" />Customer Friendly</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><br /><input type="checkbox" name="check1[]" value="Others" />Others</td>
 </tr>
<input type="submit" value="submit" />
</form>

在 php

<?php 
if(isset($_POST['check1'])){
$check = $_POST['check1'];
foreach($check as $checked) {
      $query="INSERT INTO qe_qcc (checkbox) VALUES ('".$checked."')";
      mysql_query($query) or die(mysql_error());

    }
   }
 ?>