2011-08-15 176 views
0

我正在使用複選框接受多個值的特定窗體上工作,我發現很難獲取檢查框的所有值並將它們存儲到數據庫中。如何從多個複選框中獲取值並插入到數據庫中?

每當正在執行我的插入查詢..存儲在數據庫中的唯一價值是最後的複選框,這樣用戶選擇忽略其他..

這裏是我的代碼:

--- -形成 - - - - - - - - - - - - - - - - - - - - - - - - -----------------'

<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。 我使用複選框上所需的列資源..

任何幫助將是appreciate..tnx

+0

使用mysql_real_escape_string()來避免SQL注入。 – Pehmolelu

回答

5

如果更改複選框的name屬性name="resources[]"你應該在$_POST['resources']

數組

一旦你在$_POST['resources']有數組,你可以在數據庫中存儲/檢索你喜歡的數組。例如作爲使用implode()/explode()serialize()/unserialize()的字符串。

+0

'name =「resources []」'將會是正確的 – SleighBoy

+0

@SleighBoy true,fixed,thank you – piddl0r

相關問題