我有一个表格,用于收集要在通讯录中使用的人的信息。我需要创建两个表。一个用于大部分数据,另一个用于电话号码,因为每个人都可以有多个电话号码。显然我希望这些表是相关的,但我不明白如何将表单中的数据输入到两个表中,以便它们共享关系。我还需要能够将数据连接到主表中的单个电话号码列中。
这个想法是这样说明的:
(来源:gyazo.com)
使用 tableA_id 填充电话号码字段。我如何从表单中输入数据才能做到这一点?
更新代码
11 //Form Validation
12 $name = $email = $gender = $comment = $website = "";
13
14 if ($_SERVER["REQUEST_METHOD"] == "POST")
15 {
16 $firstname = test_input($_POST["firstname"]);
17 $email = test_input($_POST["email"]);
18 $lastname = test_input($_POST["lastname"]);
19 $street = test_input($_POST["street"]);
20 $city = test_input($_POST["city"]);
21 $state = test_input($_POST["state"]);
22 $country = test_input($_POST["country"]);
23 $workphone = test_input($_POST["workphone"]);
24 $mobilephone = test_input($_POST["mobilephone"]);
25 $homephone = test_input($_POST["homephone"]);
26 $phonearray = array("$workphone","$mobilephone","$homephone");
27
28
29 }
30
31 function test_input($data)
32 {
33 $data = trim($data);
34 $data = stripslashes($data);
35 $data = htmlspecialchars($data);
36 return $data;
37 }
//After validation we input the data into the tables.
84
85 $sql="INSERT INTO nameaddress
86 (FirstName, LastName, Street, City, State, Country,email,photo)
87 VALUES
88 ('$firstname','$lastname','$street','$city','$state','$country','$email','$uploadfile')";
89
90
91
92
93
94 if (!mysqli_query($con,$sql))
95 {
96 die('Error: ' . mysqli_error($con));
97 }
98
99 $lastInsertId=mysqli_insert_id($con);
100 foreach ($phonearray as $a) {
101 $select="INSERT INTO userphones(phonenumbers,nameaddress_id)102
102 VALUES ($a,".$lastInsertId.")";
103 mysqli_query($con,$select);
104 }