Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加相同的';id';在sql中使用php的两个表中_Php_Mysql_Sql_Mysqli - Fatal编程技术网

添加相同的';id';在sql中使用php的两个表中

添加相同的';id';在sql中使用php的两个表中,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,我试图在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')"); 此代码从我的HTML输入框中获取值,并将值正确地添加到表中,现在

我试图在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')");
此代码从我的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)");

有谁能告诉我,有没有什么方法可以使用php在sql中为两个表提供相同的id

使用
$last\u id=$con->insert\u id

请尝试下面的代码。这对你有用

 $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();

如评论中所述以及随后所请求的-一个
触发器
似乎是处理该问题的一个不错的选择,因为您只需要关注初始插入-然后触发器会自动处理重复的ID(或其他字段)

给出了两个基本的表格来复制问题中的这些内容

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 |       |
+-----------+------------------+------+-----+-------------------+-------+
一个简单的
触发器
,绑定到
employees
表,并在添加新行时插入到
date

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

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`;
结果

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

您可以使用
empid
。在
employees
date
表中插入
empid
。您应该在关系数据库中投入一些时间。我认为你没有抓住要点。您不需要相同的id,您只需要一个表中的一个字段来引用另一个。@BlackNetworkBit我知道,但我的表中的数据将不断添加,因此我需要一列中的随机数来引用另一列,但如果我生成随机数,它将不会100%返回唯一值,因此,我试图使用相同的id,这将是unique@BlackNetworkBit如何在没有公共值的情况下连接两个表???@Anshu Sharma这与连接无关,他想插入它们。所以他可以做裁判。