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

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法