我有一个注册表格,公司注册他们的业务以及他们经营的5个地点。公司和地点数据存储在单独的表格中,然后通过第三个关系表相互关联。
这是我的代码,但我不确定如何成功填充关系表。
例如,假设用户提交了id为3的croydon,那么关系表应该看起来像我手动填写的那个,但我找不到自动填充它的方法!
以下是我的表格外观;
公司
地点
关系表(这是一个手动输入)
// IMPORT TO DATABASE
$query = "INSERT INTO partners (partner_id, partner_name, email_address, contact_number, active, street_number, street_name, town_city, postcode, country)
VALUES
(NULL, '".$companyname."', '".$email."', '".$contactnumber."', 'yes', '".$street_number."', '".$street_name."', '".$town_city."', '".$postcode."', '".$country."')";
$connect->query($query);
//INSERT NEW LOCATIONS TO DATABASE
$insert = "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area1."')";
$insert .= ", (NULL, '".$area2."')";
$insert .= ", (NULL, '".$area3."')";
$insert .= ", (NULL, '".$area4."')";
$insert .= ", (NULL, '".$area5."')";
$insert .= ";";
if ($connect->query($insert) === TRUE) {
} else {
echo "Error: " . $insert . "<br>" . $connect->error;
}
//RELATE PARTNER TO LOCATIONS
*已解决*
// GET LOCATION ID 1
$get_location_id_one = "SELECT location_id FROM locations WHERE (location_name = '$area1')";
if ($location_ids = $connect->query($get_location_id_one)) {
foreach ($location_ids as $location_id) {
$location_id1 = $location_id['location_id'];
}
} else {
echo "Error: No Locations<br>";
}
// GET LOCATION ID 2
$get_location_id_one = "SELECT location_id FROM locations WHERE (location_name = '$area2')";
if ($location_ids = $connect->query($get_location_id_one)) {
foreach ($location_ids as $location_id) {
$location_id2 = $location_id['location_id'];
}
} else {
echo "Error: No Locations<br>";
}
// GET LOCATION ID 3
$get_location_id_one = "SELECT location_id FROM locations WHERE (location_name = '$area3')";
if ($location_ids = $connect->query($get_location_id_one)) {
foreach ($location_ids as $location_id) {
$location_id3 = $location_id['location_id'];
}
} else {
echo "Error: No Locations<br>";
}
// GET LOCATION ID 4
$get_location_id_one = "SELECT location_id FROM locations WHERE (location_name = '$area4')";
if ($location_ids = $connect->query($get_location_id_one)) {
foreach ($location_ids as $location_id) {
$location_id4 = $location_id['location_id'];
}
} else {
echo "Error: No Locations<br>";
}
// GET LOCATION ID 5
$get_location_id_one = "SELECT location_id FROM locations WHERE (location_name = '$area5')";
if ($location_ids = $connect->query($get_location_id_one)) {
foreach ($location_ids as $location_id) {
$location_id5 = $location_id['location_id'];
}
} else {
echo "Error: No Locations<br>";
}
$insert = "INSERT INTO partners_locations (partners_locations_id, partner_id, location_id) VALUES (NULL, '".$partner_id."', '".$location_id1."')";
$insert .= ", (NULL, '".$partner_id."', '".$location_id2."')";
$insert .= ", (NULL, '".$partner_id."', '".$location_id3."')";
$insert .= ", (NULL, '".$partner_id."', '".$location_id4."')";
$insert .= ", (NULL, '".$partner_id."', '".$location_id5."')";
$insert .= ";";
$connect->query($insert);
答案 0 :(得分:0)
要将一行插入&#34;关系&#34;表,您需要从插入partner_id
(公?)表的行中分配给partners
列的值。
如果该列是AUTO_INCREMENT列...您可以检索使用MySQL LAST_INSERT_ID
函数分配的值。
参考:http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
成功插入后,您可以立即运行查询SELECT LAST_INSERT_ID()
。大多数数据库扩展都公开了相同的功能,因此您不必使用额外的SQL语句来破坏代码。数据库扩展将处理运行SELECT并返回值。
例如:
您可以在INSERT中将检索到的值用于&#34;关系&#34;表
我强烈建议您使用预备语句和绑定占位符。您发布的代码似乎容易受到SQL注入攻击(将可能不安全的值合并到SQL文本中。)
如果您不使用绑定占位符,则至少应该通过转义函数运行值...