dongluanguang4774 2011-01-03 20:42
浏览 65
已采纳

PHP Mysql增量外键

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;
  • 写回答

2条回答 默认 最新

  • duanbangzhou7809 2011-01-03 22:26
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序