开发者

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>
开发者_JAVA百科<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. I am using checkbox on the column resources needed..

any help will be appreciate..tnx


If you change the name attribute of the checkbox to name="resources[]" you should get an array in the $_POST['resources']

Once you have the array in $_POST['resources']you can store/retrieve it in the database how you like. For example either as a string using implode() / explode() or serialize() / unserialize().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜