tom7890 Posted January 22, 2015 Share Posted January 22, 2015 I have a table made up of time slots, when the user clicks maybe one or two and presses submit, i would like the date selected and the time slots chosen to be saved in to my sql. I am working on the query but a little stuck in regards to the query, i have done the following: <?php $username = "root"; $password = ""; $hostname = "localhost"; $dbhandle = mysql_connect($hostname, $username, $password) or die ("no connection to database"); if(isset($_POST['Submit'])){ $start = mysql_real_escape_string($_POST['start']); $end = mysql_real_escape_string($_POST['end']); $booked = mysql_real_escape_string($_POST['booked']); $selected = mysql_select_db("booking", $dbhandle); ?> i have set the table as follows <input data-val='08:30 - 08:45' class='fields' type='checkbox' name="booked[]" /> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 $booked = mysql_real_escape_string($_POST['booked']); You don't set any values for your checkbox inputs, so $_POST['booked'] isn't transmitting anything. Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 i have amended that but now i have an error the data saved into the database is incorrect it shows:  i selected 17:45-18:00 but in the database it is shown as:  start    end 00:00:  00:00: Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 Show the new code Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 <?php $username = "root"; $password = ""; $hostname = "localhost"; $dbhandle = mysql_connect($hostname, $username, $password) or die ("no connection to database"); if(isset($_POST['Submit'])){ $start = mysql_real_escape_string($_POST['start']); $end = mysql_real_escape_string($_POST['end']); $selected = mysql_select_db("booking", $dbhandle); $insert = mysql_query("INSERT into booking (start, end) VALUES ('".$start."','".$end."')"); if($insert) echo "Success"; else echo "There was an error " . mysql_error(); } mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 And where is 'start' and 'end' coming from? Show the HTML as well. You're familiar with your code. We only see what you post and you aren't posting enough info for us to be helpful. Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) html code..  start and end are the column names, i have doubled it up on the page as it was toooo long <form method="post" > <div align="center"> <table width='400' border='6' cellpadding='2' cellspacing='2' id='timeslots' style="display:none;"'> <tr> <th width='71' height="32" bgcolor="#999999" border='6 align='left'><div align="center">Start</div></th> <th width='70' align='left' bgcolor="#999999"><div align="center">End</div></th> <th width='38' align='left' bgcolor="#999999"><div align="center">Book</div></th> <th width='6' align='left' bgcolor="#666666"> </th> <th width='115' align='left' bgcolor="#999999"><div align="center">Start</div></th> <th width='63' align='left' bgcolor="#999999"> <div align="center">End</div></th> <th width='41' align='left' bgcolor="#999999"><div align="center">Book</div></th> </tr> <tr> <td height="26"><div align="center">08:00 </div></td> <td><div align="center">08:15</div></td> <td><div align="center"> <input data-val='08:00 - 08:15' class='fields' type='checkbox' name="booked[]"/> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">08:15</div></td> <td><div align="center">08:30</div></td> <td><div align="center"> <input data-val='08:15 - 08:30' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">08:30</div></td> <td><div align="center">08:45</div></td> <td><div align="center"> <input data-val='08:30 - 08:45' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">08:45</div></td> <td><div align="center">09:00 </div></td> <td><div align="center"> <input data-val='08:45 - 09:00' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">09:00</div></td> <td><div align="center">09:15</div></td> <td><div align="center"> <input data-val='09:00 - 09:15' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">09:15</div></td> <td><div align="center">09:30</div></td> <td><div align="center"> <input data-val='09:15- 09:30' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">09:30</div></td> <td><div align="center">09:45</div></td> <td><div align="center"> <input data-val='09:30:00 - 09:45' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">09:45</div></td> <td><div align="center">10:00</div></td> <td><div align="center"> <input data-val='09:45 - 10:00' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">10:00</div></td> <td><div align="center">10:15</div></td> <td><div align="center"> <input data-val='10:00 - 10:15' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">10:15</div></td> <td><div align="center">10:30</div></td> <td><div align="center"> <input data-val='10:15 - 10:30' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">10:30</div></td> <td><div align="center">10:45</div></td> <td><div align="center"> <input data-val='10:30 - 10:45' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">10:45</div></td> <td><div align="center">11:00</div></td> <td><div align="center"> <input data-val='09:30:00 - 10:00:00' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">11:00</div></td> <td><div align="center">11:15</div></td> <td><div align="center"> <input data-val='11:00 - 11:15' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">11:15</div></td> <td><div align="center">11:30</div></td> <td><div align="center"> <input data-val='11:15 - 11:30' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">11:30</div></td> <td><div align="center">11:45</div></td> <td><div align="center"> <input data-val='11:30 - 11:45' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">11:45</div></td> <td><div align="center">12:00</div></td> <td><div align="center"> <input data-val='11:45 - 12:00' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">12:00</div></td> <td><div align="center">12:15</div></td> <td><div align="center"> <input data-val='12:00 - 12:15' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">12:15</div></td> <td><div align="center">12:30</div></td> <td><div align="center"> <input data-val='12:15 - 12:30' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">12:30</div></td> <td><div align="center">12:45</div></td> <td><div align="center"> <input data-val='12:30 - 12:45' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">12:45</div></td> <td><div align="center">13:00</div></td> <td><div align="center"> <input data-val='12:45 - 13:00' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">13:00</div></td> <td><div align="center">13:15</div></td> <td><div align="center"> <input data-val='13:00 - 13:15' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">13:15</div></td> <td><div align="center">13:30</div></td> <td><div align="center"> <input data-val='13:15 - 13:30' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">13:30</div></td> <td><div align="center">13:45</div></td> <td><div align="center"> <input data-val='13:30 - 13:45' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">13:45</div></td> <td><div align="center">14:00</div></td> <td><div align="center"> <input data-val='13:45 - 14:00' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">14:00</div></td> <td><p align="center">14:15</p></td> <td><div align="center"> <input data-val='14:00 - 14:15' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">14:15</div></td> <td><div align="center">14:30</div></td> <td><div align="center"> <input data-val='14:15 - 14:30' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">14:30</div></td> <td><div align="center">14:45</div></td> <td><div align="center"> <input data-val='14:30 - 14:45' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">14:45</div></td> <td><div align="center">15:00</div></td> <td><div align="center"> <input data-val='14:45 - 15:00' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">15:00</div></td> <td><div align="center">15:15</div></td> <td><div align="center"> <input data-val='15:00 - 15:15' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">15:15</div></td> <td><div align="center">15:30</div></td> <td><div align="center"> <input data-val='15:15 - 15:30' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">15:30</div></td> <td><div align="center">15:45</div></td> <td><div align="center"> <input data-val='15:30 - 15:45' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">15:45</div></td> <td><div align="center">16:00</div></td> <td><div align="center"> <input data-val='15:45 - 16:00' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">16:00</div></td> <td><div align="center">16:15</div></td> <td><div align="center"> <input data-val='16:00 - 16:15' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">16:30</div></td> <td><div align="center">16:45</div></td> <td><div align="center"> <input data-val='16:15 - 16:45' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">16:45</div></td> <td><div align="center">17:00</div></td> <td><div align="center"> <input data-val='16:45 - 17:00' class='fields' type='checkbox' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">17:00</div></td> <td><div align="center">17:15</div></td> <td><div align="center"> <input data-val='17:00 - 17:15' class='fields' type='checkbox' name="booked[]" /> </div></td> </tr> <tr> <td height="26"><div align="center">17:15</div></td> <td><div align="center">17:30</div></td> <td><div align="center"> <input type='checkbox' value='17:15 - 17:30' name="booked[]" /> </div></td> <td bgcolor="#666666"> </td> <td><div align="center">17:30</div></td> <td><div align="center">17:45</div></td> <td><div align="center"> <input type="checkbox" value="17:30 - 17:45" name="booked[]" <br> </div></td> </tr> <tr> <td height="26"><div align="center">17:45</div></td> <td><div align="center">18:00</div></td> <td><div align="center"> <input data-val='17:45 - 18:00' class='fields' type='checkbox' name="booked[]" /> <td bgcolor="#666666"> </td> <td colspan="3"><div align="center"> <input type="Submit" name="Submit" id="Submit" value="Submit" /> </div> <div align="center"></div> <div align="center"></div></td> </tr> </table> Edited January 22, 2015 by tom7890 Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 (edited) Only form fields get transmitted in forms, not stuff from other html. There are no form fields with the name of start or end. Try this: Set the VALUE of the checkbox (the value is what gets transmitted along with the NAME) <input class='fields' type='checkbox' name="booked[]" value="08:00 - 08:15"/> Since you are naming those checkboxes 'booked[]', and allow multiple checkboxes to be submitted, you will need to loop over the array in php. //grab all transmitted "booked" entries from the $_POST array $timeslots = $_POST['booked']; //iterate over the array foreach($timeslots as $timeslot) {  //remove spaces from the timeslot as it's in the format of "xx:xx - yy:yy"  $timeslot = str_replace(' ', '', $timeslot); //Now looks like "xx:xx-yy:yy"    //get the "start" and "end" times, by splitting the string on the dash  list($start, $end) = explode('-', $timeslot); //$start now = xx:xx, $end now = yy:yy  //just echo out each start/end time to see that it's working  echo "Start: $start, End: $end<br>"; } Edited January 22, 2015 by CroNiX 1 Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) thank you, that worked. Much appreciated.  If a time has been selected and it has been added to the database how can i block this out so it cant be selected again?    2. Also am i able to get the date which has been selected from the calendar into the database too?    so it would be date selected, start time, end time     ie. tom 16:00 16:45 Edited January 22, 2015 by tom7890 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 (edited) Don't output a checkbox for times already booked. If you look at the booking form in the code I gave you you will see it only allows input for non-booked times (except for a cancellation checkbox). And add a unique key on (date, starttime) in the booking table to prevent duplicates. Add a hidden input field to your form containing the date value <input type="hidden" name="date" value="2015-01-22" /> then you can access with $_POST['date']  Why are you hard-coding all those start times in your form instead of pulling them from a timeslot table in the db? Again, if you look at my code you will see it uses a query with     FROM timeslot LEFT JOIN booking so you know all the timeslots and which timeslots have/not been booked for the day Edited January 22, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015  Don't output a checkbox for times already booked. If you look at the booking form in the code I gave you you will see it only allows input for non-booked times (except for a cancellation checkbox). And add a unique key on (date, starttime) in the booking table to prevent duplicates. Add a hidden input field to your form containing the date value <input type="hidden" name="date" value="2015-01-22" /> then you can access with $_POST['date']  Why are you hard-coding all those start times in your form instead of pulling them from a timeslot table in the db? Again, if you look at my code you will see it uses a query with     FROM timeslot LEFT JOIN booking so you know all the timeslots and which timeslots have/not been booked for the day  Barand, i need to get somthing clear.  Please can you be patient so i can understand, i know you are trying to help me but it takes time for me to understand, Your patience will be much appreciated.  I have created a table in html the code is posted above where i have added all the timings for the timeslots myself and in the database i have added a table with the same headings but not entered any data.  Is this incorrect? Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) Should he timeslots table in sql have the following headings:   Date(Unique Key) start_time end_time  do i need a column for the check boxes?   Do i need to create a table in html like i have done so above shown in previous posts with the headings: Date(Unique Key) start_time end_time  but not add any data in to the table? Edited January 22, 2015 by tom7890 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 Your timeslot table would not have a date - it is to define the daily times. You booking table would contain the date and the booked timeslot. See my diagram that i posted earlier  http://forums.phpfreaks.com/topic/293995-php-sql-calendar-add-events/?do=findComment&comment=1503204 Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 I have now updated my tables.  In the database do i need to enter all the start and end times?  Then do i take out all the times in the html table?  Will the headings in the html table be: timeslots_id, start_time, end_time Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) ignore the above post please.  Can you please tell me when i enter the data in the database for the timeslots table, in the timeslots_id column do i enter a number or is that meant to be auto incremented? Edited January 22, 2015 by tom7890 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 Make the timeslot id auto_increment  This is the code I have just used to populate my version of the timeslot table with 15 min timeslots $db = new mysqli('localhost', 'user', 'pwd', 'db'); // use your credentials $sql = "TRUNCATE TABLE timeslot"; $db->query($sql); $dt1 = new DateTime("09:00:00"); $dt2 = new DateTime("09:15:00"); $di = new DateInterval('PT15M'); $dp = new DatePeriod($dt1, $di, new DateTime('18:00:00')); $sql = "INSERT INTO timeslot(start_time, end_time) VALUES (?,?)"; $smt = $db->prepare($sql); $smt->bind_param('ss', $st,$et); foreach ($dp as $d) { $st = $d->format('H:i'); $et = $dt2->format('H:i'); $dt2->add($di); $smt->execute(); } ?> Done Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 i get this error  Undefined variable: db Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 Yes, that's HIS db object. You need to adapt the code to however YOU are connecting to and querying the database. Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) error  Call to a member function bind_param() on a non-object  what does this mean?  my code is like so: $selected = mysql_select_db("booking", $dbhandle); $sql = "TRUNCATE TABLE timeslots"; $dt1 = new DateTime("09:00:00"); $dt2 = new DateTime("09:15:00"); $di = new DateInterval('PT15M'); $dp = new DatePeriod($dt1, $di, new DateTime('18:00:00')); $query = "INSERT INTO timeslots (start_time, end_time) VALUES (?,?)"; $result = mysql_query($query); $query->bind_param('ss', $st,$et); foreach ($dp as $d) { $st = $d->format('H:i'); $et = $dt2->format('H:i'); $dt2->add($di); $query->execute(); } Edited January 22, 2015 by tom7890 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 All you had to change was this line - note the comment  $db = new mysqli('localhost', 'user', 'pwd', 'db'); // use your credentials Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 i did but it still didnt work, my full code is <?php $username = "root"; $password = ""; $hostname = "localhost"; $dbhandle = mysql_connect($hostname, $username, $password) or die ("no connection to database"); $selected = mysql_select_db("booking", $dbhandle); $sql = "TRUNCATE TABLE timeslots"; $dt1 = new DateTime("09:00:00"); $dt2 = new DateTime("09:15:00"); $di = new DateInterval('PT15M'); $dp = new DatePeriod($dt1, $di, new DateTime('18:00:00')); $query = "INSERT INTO timeslots (start_time, end_time) VALUES (?,?)"; $result = mysql_query($query); $query->bind_param('ss', $start_time,$end_time); foreach ($dp as $d) { $st = $d->format('H:i'); $et = $dt2->format('H:i'); $dt2->add($di); $query->execute(); } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015  i did but it still didnt work, my full code is   No, you didn't. You changed it from a mysqli connection to a mysql connection. Just change the credentials USERNAME PASSWORD DATABASENAME mysql_xxxxx functions are deprecated and will be withdrawn from PHP. Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 ive been using mysql connection not mysqli thats why Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 Yes, but the original mysql extension is being removed from php, so all the code you are writing today using the older mysql functions won't work with an upcoming new version of PHP. So it's really best to not use that extension on a new project and to use PDO or mysqli (like Barand showed) unless you want to rewrite all of this stuff when a newer version of PHP comes out and you upgrade to it (or your host does). Quote Link to comment Share on other sites More sharing options...
tom7890 Posted January 22, 2015 Author Share Posted January 22, 2015 okay well i have changed it now and it is working thanks barand Quote Link to comment 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.