简体   繁体   English

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

[英]How to get values from a multiple checkbox and insert into database?

I am working on a certain form that accepts multiple values using checkbox and I find it hard to get all the values of the checked boxes and store them into the database.我正在研究一种使用复选框接受多个值的表单,我发现很难获取选中框的所有值并将它们存储到数据库中。

Everytime my insertion query is being executed.. the only value that is stored in the database is the last checkbox the user selects thus ignoring the others..每次执行我的插入查询时..存储在数据库中的唯一值是用户选择的最后一个复选框,因此忽略其他复选框..

here's my code:这是我的代码:

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

<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>

------------action code---------------------------------------------------- ------------动作代码------------------------------------ ----------------

<?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. PS。 I am using checkbox on the column resources needed..我在需要的列资源上使用复选框..

any help will be appreciate..tnx任何帮助将不胜感激..tnx

If you change the name attribute of the checkbox to name="resources[]" you should get an array in the $_POST['resources']如果您将复选框的名称属性更改为name="resources[]"您应该在$_POST['resources']中获得一个数组

Once you have the array in $_POST['resources'] you can store/retrieve it in the database how you like.$_POST['resources']中拥有数组后,您可以根据需要在数据库中存储/检索它。 For example either as a string using implode() / explode() or serialize() / unserialize() .例如,使用implode() / explode()serialize() / unserialize()作为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM