简体   繁体   English

使用php在sql的两个表中添加相同的“id”

[英]adding same 'id' in two tables in sql using php

I am trying to give same id in 2 sql table, my code is like below:我试图在 2 sql 表中给出相同的 id,我的代码如下:

$field1=$_POST['field1'];
$field2=$_POST['field2'];
$field3=$_POST['field3'];

 $query=mysqli_query($con,"insert into employees(name,position,salary) value('$field1','$field2','$field3')");

This code takes values from my HTML input box and add the values to the table correctly, now i need one more table which will have the same id as the above table(so that i can connect both tables), i tried something like the below code after the first query, but as i am not able to get any condition which will give me the correct id, nothing worked, sample is below此代码从我的HTML输入框中获取值并将这些值正确添加到表中,现在我需要一个与上表具有相同 ID 的表(以便我可以连接两个表),我尝试了如下所示第一次查询后的代码,但由于我无法获得任何条件,这会给我正确的 id,没有任何效果,示例如下

$query=mysqli_query($con,"insert into employees(name,position,salary,empid) value('$field1','$field2','$field3')");

$query=mysqli_query($con,"insert into date(id) value(select id from employees where)");

Can anyone please tell me is there any way to give same id to both tables in sql using php.任何人都可以告诉我有什么方法可以使用php为sql中的两个表提供相同的ID。

As mentioned in the comment and subsequently requested - a trigger would seem a good option to deal with the problem as you need only concern yourself with the initial insert - the duplicated ID ( or other fields ) are then handled automatically by the trigger.正如评论中提到的,随后请求 - trigger似乎是处理问题的好选择,因为您只需要关注初始插入 - 然后重复的 ID(或其他字段)由触发器自动处理。

Given two basic tables to replicate thse from the question给定两个基本表来从问题中复制这些

mysql> describe employees;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name     | varchar(50)      | NO   |     | 0       |                |
| position | varchar(50)      | NO   |     | 0       |                |
| salary   | decimal(10,2)    | NO   |     | 0.00    |                |
+----------+------------------+------+-----+---------+----------------+



mysql> describe date;
+-----------+------------------+------+-----+-------------------+-------+
| Field     | Type             | Null | Key | Default           | Extra |
+-----------+------------------+------+-----+-------------------+-------+
| id        | int(10) unsigned | NO   | PRI | NULL              |       |
| timestamp | timestamp        | NO   |     | CURRENT_TIMESTAMP |       |
+-----------+------------------+------+-----+-------------------+-------+

A simple trigger that is bound to the employees table and inserts to the date table when a new row has been added.一个简单的trigger ,它绑定到employees表并在添加新行时插入到date表。

CREATE TRIGGER `tr_employee_inserts` AFTER INSERT ON `employees` FOR EACH ROW BEGIN
    insert into `date` set `id`=new.id;
END

To test去测试

insert into employees (`name`,`position`,`salary` ) values ( 'Peter', 'Porcupine Pickler', 75000 );
insert into employees (`name`,`position`,`salary` ) values ( 'Roger', 'Rabitt Rustler', 25000 );
insert into employees (`name`,`position`,`salary` ) values ( 'Michael', 'Mouse Mauler', 15000 );

select * from `employees`;
select * from `date`;

The result结果

mysql> select * from employees;
+----+---------+-------------------+----------+
| id | name    | position          | salary   |
+----+---------+-------------------+----------+
|  1 | Peter   | Porcupine Pickler | 75000.00 |
|  2 | Roger   | Rabitt Rustler    | 25000.00 |
|  3 | Michael | Mouse Mauler      | 15000.00 |
+----+---------+-------------------+----------+


mysql> select * from date;
+----+---------------------+
| id | timestamp           |
+----+---------------------+
|  1 | 2020-01-16 10:11:15 |
|  2 | 2020-01-16 10:11:15 |
|  3 | 2020-01-16 10:11:15 |
+----+---------------------+

Use $last_id = $con->insert_id;使用$last_id = $con->insert_id; for get last inserted ID.获取最后插入的 ID。

Please try the below code.请尝试以下代码。 It's working for you.它为你工作。

 $field1 = 'name';
    $field2 = 'position';
    $field3 = '10000';
    $field4 = 'SOF01';

// insert employees data
    $stmt = $con->prepare("INSERT INTO employees (name,position,salary,empid) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $field1, $field2, $field3, $field4);
    $stmt->execute();

// get last insert id
    $last_id = $con->insert_id;

// insert last id in date table
    $stmt = $con->prepare("INSERT INTO date (id) VALUES (?)");
    $stmt->bind_param("s", $last_id);
    $stmt->execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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