简体   繁体   中英

PHP Mysql Increment Foreign Key

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;

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!

查看MySQL UPDATE语句 ,它允许您更改表中一行中的字段的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM