i am currently trying to make a relational database in PHPmyadmin so i can have a user save there information in 1 table and then in a 2nd table i am trying to have a user be able to save their phone number.
I will connect the 2 tables with a foreign key of userID and/or phoneID.
I made a button on my form right now its standard 1 textbox and label for a phone number. i used javascript so i can dynamically add another textbox and label to the form to have more then one phone number for a user.
My problem is creating a database table that will automatically create another column in the table for a 2nd phone number. Also inserting data into both of the tables because i am not sure how many phone numbers a user may want to have.
You can click the button as many times as you want to keep adding textboxes for more numbers.
I just want to be able to save as many phone numbers as the user wants and to connect both tables.
Here is my code.
<form method="post" id="userProfileForm"> <fieldset> <legend>Information</legend> <div class="section"> <label for="firstName">First name:</label> <input type="text" name="firstName" id="firstName" placeholder="Enter your name" /> <br><br> <label for="lastName">Last name:</label> <input type="text" name="lastName" id="lastName" placeholder="Enter last name" /> <br><br> <label for="address">Address:</label> <input type="text" name="address" id="address" placeholder="Enter your address" /> <br><br> <label for="email">Email:</label> <input type="text" name="email" id="email" placeholder="Enter your email" /> <br> </div> <!-- all of the phone numbers --> <h5>Phone number(s)</h5> <label for="areaHome">Home Phone:</label> <input type="text" name="areaHome" id="areaHome" placeholder="Area code" /> <input type="text" name="homePhone" id="homePhone" placeholder="Phone Number" /> <br> <div id="addPhone"> <input type="button" name="addnumber" id="addnumber" value="Add Number" onclick="NewPhoneData()" /> <!-- ; --> <br> </div> with a submit button at the bottom function NewPhoneData() { var lblPhone, txtPhone; lblPhone = document.createElement('label'); lblPhone.appendChild(document.createTextNode("Phone Number "+txtboxcounter)); txtPhone = document.createElement('input'); txtPhone.type = 'text'; lblPhone.appendChild(txtPhone); document.getElementById('addPhone').appendChild(lblPhone); txtboxcounter++; } //phone number field if (empty($_POST['homePhone'])) { echo 'ERROR: Please enter a valid employee name'; $inputError = true; } else { // make sure the string is properly escaped $homePhone = $mysqli->escape_string($_POST['areaHome'] . $_POST['homePhone']); //echo "It Worked!!"; } if ($inputError != true) { // build our insert query -- we can put php variables directly in the string $sql = "INSERT INTO people (address, email, firstName, lastName, mainPhone) VALUES ('$address', '$email', '$fName', '$lName', '$homePhone')"; if ($mysqli->query($sql) === true) { echo 'New employee record added with ID: ' . $mysqli->insert_id; } else { echo "ERROR: Could not execute query: $sql. " . $mysqli->error; } }