Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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.
HTML
<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="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><input type="submit" id="submit" name="submit" value="Submit" />
				</fieldset>
			</form>
		</div>

JavaScript
<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
<?php 
		//Connecting to the database.
			$mysqli = new mysqli("localhost", "root", "", "users");
		    if ($mysqli === false) 
		    {
		      die("ERROR: Could not connect to database. " . mysqli_connect_error());
		    }
	//when submit button is clicked
		    if (isset($_POST['submit'])) 
    		{ 
    			$inputError = false;
    			//firstname field
	    		if (empty($_POST['firstName'])) 
	      		{
		        	echo 'ERROR: Please enter a valid firstname';
	    	    	$inputError = true;        
	    		} 
	    		else 
	      		{
	          // make sure the string is properly escaped
	      			$fName = $mysqli->escape_string($_POST['firstName']);
	      			//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')";
					 
					 $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;
			        }
Posted

You can put the insert script for the contact number in a for loop
and since the foreign key for all the rows will be same ,you can use the same id value
for all the rows

for the later you can use

SELECT MAX(id coumn name) FROM table
 
Share this answer
 
Hello,

I think here you can crate a separate table for storing the phone numbers.Storing new rows in the same table just for different phone numbers would waste much of the space.
As for retrieving the auto generated id field i think you can get get the max() id value in the table.

Please let me know if this works for you.
 
Share this answer
 
v2
Comments
TheBigBearNow 5-Oct-14 3:31am    
could you give me an example?
for making the rows in the table for multiple numbers if the rows are not already created? im quiet confused with this concept
making spots for the data in the database on the fly...
and how will i find the max id for 1 table if i need to use it as the id in the other table?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900