将checkbox的值保存到数据库中

时间:2015-01-05 07:00:37

标签: php html mysql codeigniter

这是我的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>&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>

4 个答案:

答案 0 :(得分:1)

首先不要使用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(&#39;,&#39;,$ _ POST [&#39; check1&#39;]);

答案 1 :(得分:0)

试试这个:

   $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());

        }

答案 2 :(得分:0)

你可以使用implode

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

答案 3 :(得分:0)

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());

    }
   }
 ?>