简体   繁体   English

从数组中插入多个值,然后使用PHP和MySQL将它们与单个值相关联

[英]Inserting multiple values from an array and associating them with a single value using PHP and MySQL

I am parsing a JSON file with PHP and inserting the values into a MySQL database. 我正在用PHP解析JSON文件,并将值插入MySQL数据库。

I have a table that will associate a person's ID ($constid) with the ID of a meeting they are attending. 我有一张桌子,它将一个人的ID($ constid)与他们正在参加的会议的ID相关联。 This is the Attendance table. 这是出勤表。

The issue I am having is that there are several meeting IDs tied to each person. 我遇到的问题是每个人都有多个会议ID。

The meeting IDs are stored in an array. 会议ID存储在数组中。

Here is the code I am working with to create a table of just the people (constituents): 这是我用来创建仅包含人员(组成部分)表的代码:

foreach($json['Constituents'] as $const) {
$dist = $const['District'];
$firstname = $const['FirstName'];
$lastname = $const['LastName'];
$constid = $const['Id'];

$sql = "INSERT INTO Constituents (FirstName, LastName, District, Const_Id)
VALUES ('$firstname', '$lastname', '$dist', '$constid')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully <br />";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

  foreach ($const['MeetingIds'] as $meetids) {
    echo $meetids;
  }

This is the code that inserts into the Attendance table (still within the foreach loop here): 这是插入到“出勤表”中的代码(仍在此处的foreach循环中):

 $sql = "INSERT INTO Attendance (constid, meetid)
VALUES ('$constid', '$meetids')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully <br />";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}

When I insert the meeting IDs, only the first value of each $meetid array is used. 当我插入会议ID时,仅使用每个$ meetid数组的第一个值。

There are 70 people, and 235 meetings, but my table ends up with only 70 rows. 有70人和235个会议,但我的桌子最后只有70行。

When I experimented with inserting only the meeting IDs, it did grab all 235 Meeting IDs and put them in separate rows. 当我尝试仅插入会议ID时,它确实捕获了所有235个会议ID并将它们放在单独的行中。

I would like populate my table with essentially the following: 我想在表格中填充以下内容:

ConstID | MeetingID
--------------------
12345    | 11111
12345    | 22222
12345    | 33333

How can I accomplish this? 我该怎么做?

You need to either loop through the $const['MeetingIds'] and run a new insert query for each one, or you need to dynamically build a multi-insert query. 您需要循环$const['MeetingIds']并为每个查询运行一个新的插入查询,或者您需要动态构建多插入查询。 This is the easier one, but less performant: 这是较简单的方法,但性能较差:

foreach ($const['MeetingIds'] as $meetids) {

    $sql = "INSERT INTO Attendance (constid, meetid) VALUES ('$constid', '$meetids')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully <br />";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

}

The way it is now, your query is outside the loop, so you only get the last value from the foreach iteration. 现在,查询不在循环内,因此您只能从foreach迭代中获取最后一个值。

Alternatively, you can dynamically build a single query within the foreach loop, and then execute it once outside the loop. 或者,您可以在foreach循环内动态构建单个查询,然后在循环外执行一次。

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

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