Jump to content

Recommended Posts

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[]" />

<?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();


?>

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 by tom7890

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 by CroNiX
  • Like 1

thank you, that worked. Much appreciated. 

 

  1. 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 by tom7890
  1. 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.
  2. 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 by Barand

 

  1. 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.
  2. 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? 

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 by tom7890

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

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 

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 by tom7890

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

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 by tom7890

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();
}

?>

 

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.

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).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.