如何从多个复选框中获取值并插入数据库?

时间:2011-08-15 08:18:32

标签: php html

我正在使用复选框接受多个值的某个表单,我发现很难获得所选复选框的所有值并将它们存储到数据库中。

每次执行插入查询时...数据库中存储的唯一值是用户选择的最后一个复选框,因此忽略其他复选框。

这是我的代码:

----形式------------------------------------------ -----------------------'

<form name="addRes" method="post" action="addReservation_code.php">
<table>
    <tr><td>Activity Date:</td>
    <td><select name="month">
  <option value="January">January</option>
  <option value="February">February</option>
  <option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value"December">December</option>

</select>
<select name="day">
<?php for($ctr=1;$ctr<=31;$ctr++){ ?>
<option value="<?php print($ctr); ?>"><?php print($ctr); ?></option>
<?php } ?>
</select> -
<select name="day2">
<?php for($ctr=1;$ctr<=31;$ctr++){ ?>
<option value="<?php print($ctr); ?>"><?php print($ctr); ?></option>
<?php } ?>
</select>
<input type="text" name="year"/></td>
</tr>
    <tr><td>Activity Name:</td><td><input type="text" name="act_name" /></td></tr>
    <tr><td>Activity Time:</td><td><input type="text" name="act_time" /></td></tr>
    <tr><td>Room:</td>
    <td>
     <select name="room">
      <?php 
       include 'RRSdbconnect.php';
        $query = "SELECT room_name from room";
        $result=mysql_query($query) or die('Invalid query'.mysql_error()); 
         while ($row = mysql_fetch_array($result, MYSQL_BOTH)) 
         {
          echo "<option>$row[room_name]";
         } 
      ?>
      </option>
     </select>
    </td>
    </tr>
    <tr>
     <td>Resources Needed:</td>
        <td><input type="checkbox" name="resources" value="cpu" />CPU<br />
        <input type="checkbox" name="resources" value="lcd" />LCD<br />
        <input type="checkbox" name="resources" value="sounds" />Sounds<br />
        <input type="checkbox" name="resources" value="sounds" />Others, Pls. specify<input type="text" name="others" /></td>
    </tr>
    <tr><td>No. of Person</td><td><input type="text" name="noOfPerson"/></td></tr>
    <tr><td>Reserved by:</td><td><input type="text" name="reservedby" /></td></tr>
    <tr><td></td></tr>
    <tr><td><input type="submit" name="submit" value="Submit"/><input type="reset" name="clear" value="Clear"/></td></tr>
</table>
</form>

------------动作代码--------------------------------- -------------------

<?php
include 'RRSdbconnect.php';
session_start();


$date =  $_POST['month'] . ' ' .$_POST['day']. '-' .$_POST['day2']. ', ' . $_POST['year'] ;
$name=$_POST['act_name'];
$time=$_POST['act_time'];
$room=$_POST['room'];
$resources=$_POST['resources'];
$noOfPerson=$_POST['noOfPerson'];
$reservedby=$_POST['reservedby'];


//insertions for add reservations
 $query="insert into reservation (activity_date, activity_name, activity_time, room, resources_needed, no_person, reserved_by) values('$date','$name','$time','$room','$resources','$noOfPerson','$reservedby')";
 $result=mysql_query($query,$link) or die("Error!".mysql_error());  
?>

PS。  我在所需的列资源上使用复选框..

任何帮助都会受到赞赏..tnx

1 个答案:

答案 0 :(得分:5)

如果您将复选框的name属性更改为name="resources[]",则应在$_POST['resources']

中获取一个数组

$_POST['resources']中拥有数组后,您可以按照自己喜欢的方式在数据库中存储/检索它。例如,使用implode() / explode()serialize() / unserialize()作为字符串。