I have a form that gathers information about a person to be used in a contact book. I need to create two tables. One for the majority of the data and another for phone numbers alone because each person can have multiple phone numbers. Obviously I want these table to be relatable but I do not understand how to input the data from the form into both tables so they share a relationship. I also need to be able to join the data into a single phonenumbers column in the main table.
The idea is illustrated by this:
(source: gyazo.com)
Use the tableA_id to populate the phonenumbers fields. How to i input the data from the form to be able to do this?
Updated code
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 }