0

how do i manually increment foreign key. ive got 2 tables

table times

id-timeslot   times
------------------
   1       10:00
   2       10:30
   3       10:45
   4       11:00
   5       11:15
   6       11:30

table book i have for a moment

id-book   id-timeslot
-------------------
   1         1
   2         1  

Dropdown menu (minutes to spend)

<select name="minutes_booking" id="minutes_booking">
        <option value="0">15 Minutes</option>
        <option value="1">30 Minutes</option>
        <option value="2">45 Minutes</option>
        </select>

lets say, the user select for 10:00 and "30 minutes to spend for the treatment" i want look like

   id-book   id-times
    -------------------
       1         1
       2         2   

php code:

if($minutes_booking == 0) :

     $mysqli->query("INSERT INTO doc_a(id_book, id_timeslot) 
     VALUES  ('','" . $id_timeslot . "')" ); 

     echo 'Booking has been added';

elseif($minutes_booking == 1) :


      $mysqli->query("INSERT INTO doc_a(id_book, id_timeslot) 
      VALUES  ('', '" . $id_timeslot . "')" ); 
      $mysqli->query("INSERT INTO doc_a(id_book, id_timeslot) 
      VALUES  ('', '" . $id_timeslot++ . "')" ); 
  echo 'Booking has been added';

 endif;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tonoslfx
  • 3,422
  • 15
  • 65
  • 107

2 Answers2

1

First you need to switch the id_book field to auto increment. Do you have access to phpMyAdmin or some tool to modify the database tables? Then you may want to use a structure more like this. It could be better than this, but I am trying to keep things simple.

switch ($_POST['minutes_booking']) {
   case '0':
      $mysqli->query('INSERT INTO doc_a (id_timeslot) VALUES ('.$_POST['id_timeslot'].')');
      break;
   case '1':
      $mysqli->query('INSERT INTO doc_a (id_timeslot) VALUES ('.$_POST['id_timeslot'].')');
      $mysqli->query('INSERT INTO doc_a (id_timeslot) VALUES ('.++$_POST['id_timeslot'].')');
      break;
}

If this works for you, please select the check mark next to this answer. Thanks!

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
0

Look at the MySQL UPDATE statement, which allows you to change the value of a field in a row in a table.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055