Hello
I am currently working on a simple form along with creating a relational database.
Right now i currently have a form that enters data into 1 db without any issues it works great!
Now i want to upgrade this into 2 tables.
First off making the userID in both tables as a foreign key im not sure exactly how to have the ID be connected because i have the userID in first table automatically create itself and auto increment itself.
thats 1 small issue ill work on figuring out
BUT HERE IS WHAT MY MAIN ISSUE AND QUESTION IS.
To insert into 2 tables i assume just create 2 separate sql querys and run then both. I dont think that should be to big of an issue.
BUT now i have a button that dynamically creates a textbox with the name phonenumbers[]
i can loop though this to get each field i assume.
But in my table i dont have fields created because i dont know how many numbers a person will want to have you can click the button a bunch of times and enter a bunch of numbers so there could be many
How do i create rows/columns in my database on the fly depending on how many textboxes there are with data in them?
Here is what i currently have.
<div>
<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><h5>Phone number(s)</h5>
<label for="areaHome">Home Phone:</label>
<!--
<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><input type="submit" id="submit" name="submit" value="Submit" />
</fieldset>
</form>
</div>
<script type="text/javascript" >
var txtboxcounter = 1;
function NewPhoneData()
{
var lblPhone, txtPhone;
var breakline = document.createElement("br");
lblPhone = document.createElement('label');
lblPhone.appendChild(document.createTextNode("Phone Number "+txtboxcounter));
txtPhone = document.createElement('input');
txtPhone.type = 'text';
txtPhone.id = 'number'+txtboxcounter;
txtPhone.name = "phoneNumber[]";
lblPhone.appendChild(txtPhone);
document.getElementById('addPhone').appendChild(lblPhone);
document.getElementById('addPhone').appendChild(breakline);
txtboxcounter++;
}
<?php
$mysqli = new mysqli("localhost", "root", "", "users");
if ($mysqli === false)
{
die("ERROR: Could not connect to database. " . mysqli_connect_error());
}
if (isset($_POST['submit']))
{
$inputError = false;
if (empty($_POST['firstName']))
{
echo 'ERROR: Please enter a valid firstname';
$inputError = true;
}
else
{
$fName = $mysqli->escape_string($_POST['firstName']);
}
if ($inputError != true)
{
$sql = "INSERT INTO people (address, email, firstName, lastName, mainPhone)
VALUES ('$address', '$email', '$fName', '$lName', '$homePhone')";
$sql2 = "INSERT INTO phonenumbers ()";
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;
}