PHP Mysql 递增外键

发布于 2024-10-10 19:15:23 字数 1308 浏览 1 评论 0原文

我如何手动增加外键。我有 2 张桌子

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

我有一段时间的表书

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

下拉菜单(花费的分钟数)

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

比方说,用户选择 10:00 和“治疗花费 30 分钟” 我想要看起来像

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

php 代码:

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2024-10-17 19:15:23

首先,您需要将 id_book 字段切换为自动递增。您可以访问 phpMyAdmin 或某些工具来修改数据库表吗?那么你可能想使用更像这样的结构。它可能比这更好,但我试图让事情变得简单。

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

如果这对您有用,请选中该答案旁边的复选标记。谢谢!

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!

肤浅与狂妄 2024-10-17 19:15:23

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文