Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.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
Php 如何将一个表的每次迭代的id插入另一个表_Php_Mysql - Fatal编程技术网

Php 如何将一个表的每次迭代的id插入另一个表

Php 如何将一个表的每次迭代的id插入另一个表,php,mysql,Php,Mysql,我有三张桌子票和一张测试票。在票证中,我插入了两个日期的数据,并且有不同的id,如 TICKET TABLE (DATA INSERT) ID DATE 1 2019-08-01 2 2019-08-08 这两个表都很好,但在testticket表中,我希望在TICKET表中插入数据时,在TICKET表的每个id上都应该有数据testticket 例如,数据应该是这样的 Test ticket table ID | DATE |ticke

我有三张桌子票和一张测试票。在票证中,我插入了两个日期的数据,并且有不同的id,如

TICKET TABLE (DATA INSERT)

ID      DATE

1     2019-08-01

2     2019-08-08
这两个表都很好,但在
testticket
表中,我希望在TICKET表中插入数据时,在TICKET表的每个id上都应该有数据
testticket

例如,数据应该是这样的

Test ticket table

ID     | DATE        |ticketid  |itenname

1     2019-08-01     |  1        |Computer

2     2019-08-08     |  2        |Computer    

3     2019-08-01     |  1        |Computer1

4     2019-08-08     |  2        |Computer1
但是当我使用这个代码时,它在所有测试记录中显示了
ticketid
1

$sql="select * from items";
$result = mysqli_query($GLOBALS['conn'] ,$sql );
$dates = splitDates($min, $max,$parts);

foreach ($dates as $value) {
  $date= "$value";
    $sql1="insert into tickets (date) values('$date')";
 if ($GLOBALS['conn']->query($sql1) === TRUE) {

 $last_id = $GLOBALS['conn']->insert_id;


    while($row = mysqli_fetch_array($result)){ 

   foreach ($dates as $value) {
    // $i++;
    $name= $row['itemtype'];
    $date= "$value";
    $sql="insert into glpi_testticket (name,date,tid) 
   values('$name','$date','$last_id')";

    $query3 = mysqli_query($GLOBALS['conn'],$sql ) or 
    die(mysqli_error($GLOBALS['conn']));
    }
  }
 }
}

考虑以下几点:

DROP TABLE IF EXISTS ticket;
CREATE TABLE ticket
(ticket_id SERIAL PRIMARY KEY
,date DATE NOT NULL
);

INSERT INTO ticket VALUES
(1,'2019-08-01'),
(2,'2019-08-08');

DROP TABLE IF EXISTS item;
CREATE TABLE item
(item_id SERIAL PRIMARY KEY
,item_type VARCHAR(12) NOT NULL UNIQUE
);

INSERT INTO item VALUES
(1,'Computer'),
(2,'Computer1');

SELECT * FROM ticket;
+-----------+------------+
| ticket_id | date       |
+-----------+------------+
|         1 | 2019-08-01 |
|         2 | 2019-08-08 |
+-----------+------------+
2 rows in set (0.00 sec)

SELECT * FROM item;
+---------+-----------+
| item_id | item_type |
+---------+-----------+
|       1 | Computer  |
|       2 | Computer1 |
+---------+-----------+
2 rows in set (0.00 sec)

SELECT * FROM ticket,item;
+-----------+------------+---------+-----------+
| ticket_id | date       | item_id | item_type |
+-----------+------------+---------+-----------+
|         1 | 2019-08-01 |       1 | Computer  |
|         2 | 2019-08-08 |       1 | Computer  |
|         1 | 2019-08-01 |       2 | Computer1 |
|         2 | 2019-08-08 |       2 | Computer1 |
+-----------+------------+---------+-----------+

在这个结果的形成过程中,只有一个选择受到了损害。它也可以是一个插入。

我不明白第一个查询的要点。请看:您的数据模型一开始似乎没有什么意义。为什么testticket表再次包含日期和itemname?这些已经保存在另外两个表中了,那么为什么会有这种冗余呢?整个代码相当混乱。$dates上的foreach循环(我们不知道它实际上包含什么),在while循环中,首先遍历从items表读取的记录,然后在另一个foreach循环中,再次遍历$dates…很难想象这在任何方面都有实际意义。while循环只会遍历item记录一次,在外部foreach循环的第一次迭代中。在此之后,尝试从$result获取记录只会返回null,因为您已经对结果集中的所有记录循环了一次。您需要将记录指针再次定位在开始处,以便能够再次循环结果。您在插入的所有记录中都会得到
1
,因为while循环只运行一次。您仍然会得到四条新记录,这是由于您在此处创建的foreach while foreach构造造成的。@misorude您能用代码完成吗?
DROP TABLE IF EXISTS ticket;
CREATE TABLE ticket
(ticket_id SERIAL PRIMARY KEY
,date DATE NOT NULL
);

INSERT INTO ticket VALUES
(1,'2019-08-01'),
(2,'2019-08-08');

DROP TABLE IF EXISTS item;
CREATE TABLE item
(item_id SERIAL PRIMARY KEY
,item_type VARCHAR(12) NOT NULL UNIQUE
);

INSERT INTO item VALUES
(1,'Computer'),
(2,'Computer1');

SELECT * FROM ticket;
+-----------+------------+
| ticket_id | date       |
+-----------+------------+
|         1 | 2019-08-01 |
|         2 | 2019-08-08 |
+-----------+------------+
2 rows in set (0.00 sec)

SELECT * FROM item;
+---------+-----------+
| item_id | item_type |
+---------+-----------+
|       1 | Computer  |
|       2 | Computer1 |
+---------+-----------+
2 rows in set (0.00 sec)

SELECT * FROM ticket,item;
+-----------+------------+---------+-----------+
| ticket_id | date       | item_id | item_type |
+-----------+------------+---------+-----------+
|         1 | 2019-08-01 |       1 | Computer  |
|         2 | 2019-08-08 |       1 | Computer  |
|         1 | 2019-08-01 |       2 | Computer1 |
|         2 | 2019-08-08 |       2 | Computer1 |
+-----------+------------+---------+-----------+