Forum Moderators: coopster

Message Too Old, No Replies

how can i insert more than one values of check boxes into mysql?

         

Meer

11:43 am on May 15, 2009 (gmt 0)

10+ Year Member



how can i insert more than one values of check boxes into mysql?

i have table called seats in this i have these fields "seat, seat_two, seat_three, seat_four, seat_five, seat_six"

if a user wants to reserve more than one seats so he can reserve upto six. but i want this to be happen in same id if a user book 2 tickets it these goes to seat, seat_two and all the other black but in same id.

PHP CODE

<?php

$connection = mysql_connect("localhost","root","password") or die("error connect");
mysql_select_db("online_bus_project");

if(isset($_POST['team']))

{

foreach($_POST['team'] as $value) {

$insert="INSERT INTO seats (seat) VALUES ('$value')";

mysql_query($insert);

}

}

?>

HTML CODE:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">


<table width="94%" bgcolor="#cacb98" border="1" cellpadding="0" cellspacing="0">
<tbody><tr>
<td width="12%">
<input name="team[]" value="1" type="checkbox">
<font size="2" face="verdana">1</font> </td>
<td width="12%">
<input name="team[]" value="2" type="checkbox">
<font size="2" face="verdana">2</font> </td>
<td width="13%">
<input name="team[]" value="3" type="checkbox">
<font size="2" face="verdana">3</font> </td>
<td width="13%">
<input name="team[]" value="4" type="checkbox">
<font size="2" face="verdana">4</font>
</td>
<td width="20%">
<input name="team[]" value="5" type="checkbox">
<font size="2" face="verdana">5</font> </td>
</tr>
<tr>
<td width="12%">
<input name="team[]" value="6" type="checkbox">
<font size="2" face="verdana">6</font> </td>
<td width="12%">
<input name="team[]" value="7" type="checkbox">
<font size="2" face="verdana">7</font> </td>
<td width="13%">
<input name="team[]" value="8" type="checkbox">
<font size="2" face="verdana">8</font> </td>
<td width="13%">
<input name="team[]" value="9" type="checkbox">
<font size="2" face="verdana">9</font> </td>
<td width="20%">
<input name="team[]" value="10" type="checkbox">
<font size="2" face="verdana">10</font> </td>
</tr>
<tr>
<td width="12%">
<input name="team[]" value="11" type="checkbox">
<font size="2" face="verdana">11</font> </td>
<td width="12%">
<input name="team[]" value="12" type="checkbox">
<font size="2" face="verdana">12</font> </td>
<td width="13%">
<input name="team[]" value="13" type="checkbox">
<font size="2" face="verdana">13</font> </td>
<td width="13%">
<input name="team[]" value="14" type="checkbox">
<font size="2" face="verdana">14</font> </td>
<td width="20%">
<input name="team[]" value="15" type="checkbox">
<font size="2" face="verdana">15</font> </td>
</tr>
<tr>
<td width="12%">
<input name="team[]" value="16" type="checkbox">
<font size="2" face="verdana">16</font> </td>
<td width="12%">
<input name="team[]" value="17" type="checkbox">
<font size="2" face="verdana">17</font> </td>
<td width="13%">
<input name="team[]" value="18" type="checkbox">
<font size="2" face="verdana">18</font> </td>
<td width="13%">
<input name="team[]" value="19" type="checkbox">
<font size="2" face="verdana">19</font> </td>
<td width="20%">
<input name="team[]" value="20" type="checkbox">
<font size="2" face="verdana">20</font> </td>
</tr>
<tr>
<td width="12%">
<input name="team[]" value="21" type="checkbox">
<font size="2" face="verdana">21</font> </td>
<td width="12%">
<input name="team[]" value="22" type="checkbox">
<font size="2" face="verdana">22</font> </td>
<td width="13%">
<input name="team[]" value="23" type="checkbox">
<font size="2" face="verdana">23</font> </td>
<td width="13%">
<input name="team[]" value="24" type="checkbox">
<font size="2" face="verdana">24</font> </td>
<td width="20%">
<input name="team[]" value="25" type="checkbox">
<font size="2" face="verdana">25</font> </td>
</tr>
<tr>
<td width="12%">
<input name="team[]" value="26" type="checkbox">
<font size="2" face="verdana">26</font> </td>
<td width="12%">
<input name="team[]" value="27" type="checkbox">
<font size="2" face="verdana">27</font> </td>
<td width="13%">
<input name="team[]" value="28" type="checkbox">
<font size="2" face="verdana">28</font> </td>
<td width="13%">
<input name="team[]" value="29" type="checkbox">
<font size="2" face="verdana">29</font> </td>
<td width="20%">
<input name="team[]" value="30" type="checkbox">
<font size="2" face="verdana">30</font> </td>
</tr>
<tr>
<td width="12%">
<input name="team[]" value="31" type="checkbox">
<font size="2" face="verdana">31</font> </td>
<td width="12%">
<input name="team[]" value="32" type="checkbox"> <font size="2" face="verdana">32</font>
</td>
<td width="13%">
<input name="team[]" value="33" type="checkbox"> <font size="2" face="verdana">33</font>
</td>
<td width="13%">
<input name="team[]" value="34" type="checkbox"> <font size="2" face="verdana">34</font>

</td>
<td width="20%">
<input name="team[]" value="35" type="checkbox"> <font size="2" face="verdana">35</font>
</td>
</tr>
<tr>
<td width="12%">
<input name="team[]" value="36" type="checkbox">
<font size="2" face="verdana">36</font> </td>
<td width="12%">
<input name="team[]" value="37" type="checkbox">
<font size="2" face="verdana">37</font> </td>
<td width="13%">
<input name="team[]" value="38" type="checkbox">
<font size="2" face="verdana">38</font> </td>
<td width="13%">
<input name="team[]" value="39" type="checkbox">
<font size="2" face="verdana">39</font> </td>
<td width="20%">
<input name="team[]" value="40" type="checkbox"> <font size="2" face="verdana">40</font>
</td>
</tr>
<tr>
<td width="12%">
<input name="team[]" value="41" type="checkbox"> <font size="2" face="verdana">41</font>
</td>
<td width="12%">&nbsp; </td>
<td width="13%">&nbsp; </td>
<td width="13%">&nbsp; </td>
<td width="20%"> </td>
</tr>
</tbody></table>
<p align="center">&nbsp;</p>
<p></p><table>
<tbody><tr>
<td>

<div align="center"><input type="submit" name="submit" value="submit">
</div></td>
</tr>

</tbody></table>
<br>
</form></td>
</tr>
</tbody></table></div>

dreamcatcher

6:08 am on May 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Meer,

Why not just store the values of which boxes were checked by imploding [uk3.php.net] the array and storing the ids in a single database field?

seats varchar(20) not null default '';

When you process you would insert like this:

INSERT INTO table (seats) values ('".(!empty($_POST['team']) ? implode(',',$_POST['team']) : '')."');

This stores the seats as something like 1,12,15,23

Nice easy way to do things. Just a thought.

dc

rocknbil

3:37 pm on May 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how can i insert more than one values of check boxes into mysql?

i have table called seats in this i have these fields "seat, seat_two, seat_three, seat_four, seat_five, seat_six"

Google for sql normalization, this is not the "right" way to do this. What if your "system" expands to eight seats? Or ten? "It will always be six" is okay for today, or this customer . . .

table customers . . .
id¦cust_id¦fname¦lname¦.....

table seats


id..¦..cust_id...¦...seat_num¦
2...¦..123.......¦1..........¦
3...¦..123.......¦3..........¦
4...¦..123.......¦4..........¦
5...¦..127.......¦5..........¦
6...¦..129.......¦2..........¦
7...¦..130.......¦6..........¦

You join on the cust_id field. So when someone places a booking, whatever checkboxes they have selected gets an entry in the seats table, allowing for multiple reservations or a single one. Expandable and normalized.