dannyone Posted March 25, 2009 Share Posted March 25, 2009 ok il try to explain the best i can. i have a drop down list which i can choose a room from. once i have chosen the room i then want to be able to choose times from a list of checkboxes and update the records in my database. The checkboxes will be in a table like mon tue wed 9 checkbox checkbox 10 checkbox 11 etc... i simply want to update 1 field in my db from 0 to 1, when that checkbox is ticked. my db looks like room, time, day, int 1 9 M 0 i want the rooms details selected in the dropdown to be updated so that int would change to 1. so room, time, day, int 1 9 M 1 i have all the fields in the database and i can do the dropdown, but i havent got a clue with the checkboxs and updating the db. i had help previously to insert the data into a db using mulit dimensional arrays, but its not the right design for this page i have, i just need to update a field. any help would be great Thanks Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/ Share on other sites More sharing options...
Brian W Posted March 25, 2009 Share Posted March 25, 2009 do you already have the checkboxes? Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-793774 Share on other sites More sharing options...
dannyone Posted March 25, 2009 Author Share Posted March 25, 2009 yeah sorry these are the checkboxes i was using <body> <table width="200" border="1"> <tr> <td> </td> <td>09:00</td> <td>10:00</td> <td>11:00</td> <td>12:00</td> <td>13:00</td> <td>14:00</td> <td>15:00</td> </tr> <tr> <td><div align="center">Monday</div></td> <td> <div align="center"> <input type="checkbox" name="Monday910[][]" value="whatever" /> </div> </form> </td> <td><div align="center"> <input type="checkbox" name="Monday1011[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Monday1112[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Monday121[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Monday12[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Monday23[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Monday34[][]" value="whatever" /> </div></td> </tr> <tr> <td><div align="center">Tuesday</div></td> <td><div align="center"> <input type="checkbox" name="Tuesday910[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Tuesday1011[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Tuesday1112[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Tuesday121[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Tuesday12[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Tuesday23[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Tuesday34[][]" value="whatever" /> </div></td> </tr> <tr> <td><div align="center">Wednesday</div></td> <td><div align="center"> <input type="checkbox" name="Wednesday910[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Wednesday1011[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Wednesday1112[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Wednesday121[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Wednesday12[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Wednesday23[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Wednesday34[][]" value="whatever" /> </div></td> </tr> <tr> <td><div align="center">Thursday</div></td> <td><div align="center"> <input type="checkbox" name="Thursday910[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Thursday1011[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Thursday1112[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Thursday121[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Thursday12[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Thursday23[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Thursday34[][]" value="whatever" /> </div></td> </tr> <tr> <td><div align="center">Friday</div></td> <td><div align="center"> <input type="checkbox" name="Friday910[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Friday1011[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Friday1112[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Friday121[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Friday12[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Friday23[][]" value="whatever" /> </div></td> <td><div align="center"> <input type="checkbox" name="Friday34[][]" value="whatever" /> </div></td> </tr> </body> im not sure how im going to work them though thanks Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-793779 Share on other sites More sharing options...
Brian W Posted March 25, 2009 Share Posted March 25, 2009 why are there names like "Monday910[][]" rather than "Monday[910][]" if you had em like that you could say <?php $days = array("Monday", "Tuesday","Wednesday","Thursday","Friday"); $syntax = ""; foreach($days as $day){ foreach($_GET[$day] as $box_name => $box_val){ $box_val = intval($box_val)+0; //do this because if it has no value u want it to be zero $box_name = mysql_real_escape_string($box_name); //other code $syntax .= " `$box_name` = $box_val,"; } } $syntax = rtrim(",", $syntax); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-793790 Share on other sites More sharing options...
dannyone Posted March 26, 2009 Author Share Posted March 26, 2009 Thanks again Brian. i have tried using your code but i just couldn't get my head around it, im not that good with php/mysql. i have been reading up and i have this now, but the problem is it updates all the records to which room is selected. i just want it to update the checkbox selected, can any1 advise me how i would this? <html> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> <?php mysql_connect("localhost", "danny", "danny") or die("Connection Failed"); mysql_select_db("tutorial")or die("Connection Failed"); ?> <br> <?php $car = $_POST['car']; mysql_connect("localhost", "danny", "danny") or die(mysql_error()); echo "Connected to MySQL<br />"; mysql_select_db("tutorial") or die(mysql_error()); echo "Connected to Database<br /><br />"; $result1 = mysql_query("UPDATE test SET status='$car' WHERE status='0' AND Room_ID='101'") or die(mysql_error()); $query = "SELECT * FROM test WHERE Room_ID = '101'"; $result = mysql_query($query); while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> <input type="checkbox" name="car" value="1"><?php echo $line[day], $line[timeslot], $line[Room_ID]?> <br> <?php if ($_POST['car']=="1") { echo "YES<br>"; } else { echo "NO<br>"; } } ?> </body> </html> <input type="submit" value="submit" name="submit"> <br /> <br /> </form> </html> </form> </table> </form> </html> my db is room_id, timeslot, day, status 1 9-10 Monday 0 the select statement works and shows all the correct room details and checkboxes, but when i click on 1 checkbox it updates the status of all of them. do i need a foreach loop or something? thanks Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-794396 Share on other sites More sharing options...
redarrow Posted March 26, 2009 Share Posted March 26, 2009 have you tried checked="cheeked" <<< yet? Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-794403 Share on other sites More sharing options...
dannyone Posted March 26, 2009 Author Share Posted March 26, 2009 i havn't redarrow, hope you don't mind but how do i use that? thanks Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-794408 Share on other sites More sharing options...
redarrow Posted March 26, 2009 Share Posted March 26, 2009 look at this mate and study the code <?php if(isset($_POST['submit'])){ $a=$_POST['a']; $b=$_POST['b']; $c=$_POST['c']; echo "$a\n $b\n $c\n "; } $a=array("red","blue","green"); $b=array("1","2","3"); $c=array("yes","no"); ?> <form method="POST" action=" "> <select name="a"> <option value=" ">Select</option> <?php for($i=0; $i<count($a); $i++){ echo "<option value='{$a[$i]}' CHECKED='CHECKED'>{$a[$i]}</option>"; } ?> </select> <select name="b"> <option value=" ">Select</option> <?php for($i=0; $i<count($b); $i++){ echo "<option value='{$b[$i]}' CHECKED='CHECKED'>{$b[$i]}</option>"; } ?> </select> <select name="c"> <option value=" ">Select</option> <?php for($i=0; $i<count($c); $i++){ echo "<option value='{$c[$i]}' CHECKED='CHECKED'>{$c[$i]}</option>"; } ?> </select> <input type="submit" name="submit" value="SEND!"> Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-794416 Share on other sites More sharing options...
dannyone Posted March 26, 2009 Author Share Posted March 26, 2009 i have been trying to use the tutorial from the php freaks tutorial for checkboxes and for some reason it wont work. i have created the tables properly and it connects to my database, however it wont select any of my data from the database does anyone know why? thanks Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-794480 Share on other sites More sharing options...
dannyone Posted March 26, 2009 Author Share Posted March 26, 2009 ok iv managed to do it now.. thanks every1 for their help. i just couldn't get my head around it at first! i have another question tho, once i have updated the database, i want to query the database again and disable tick the rows were status = 1, so that users can view them but not edit them. is there a simple solution? thanks Quote Link to comment https://forums.phpfreaks.com/topic/151098-solved-really-need-help-with-checkbox-update/#findComment-794590 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.