3 Replies - 660 Views - Last Post: 02 October 2014 - 01:02 PM

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 01-July 12

RelationDatabase PHPmyadmin 2 tables 1 form insert statement dynamic t

Posted 02 October 2014 - 11:05 AM

Hello
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; 
                    } 
                  }


Is This A Good Question/Topic? 0
  • +

Replies To: RelationDatabase PHPmyadmin 2 tables 1 form insert statement dynamic t

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2041
  • View blog
  • Posts: 6,264
  • Joined: 15-January 14

Re: RelationDatabase PHPmyadmin 2 tables 1 form insert statement dynamic t

Posted 02 October 2014 - 11:39 AM

You don't need another column, you need another row. If a user has 5 phone numbers then they will have 5 records in the phone numbers table each linking back to their user account.
Was This Post Helpful? 0
  • +
  • -

#3 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 01-July 12

Re: RelationDatabase PHPmyadmin 2 tables 1 form insert statement dynamic t

Posted 02 October 2014 - 12:21 PM

im not sure how to find the name of my dynamically made textbox and then ill have to make a insert statement to enter those numbers into my database
Can you please help me with inserting into a second table and figuring out the id of my dynamic txt box this is what i tried

txtPhone.id = 'number'+txtboxcounter;
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2041
  • View blog
  • Posts: 6,264
  • Joined: 15-January 14

Re: RelationDatabase PHPmyadmin 2 tables 1 form insert statement dynamic t

Posted 02 October 2014 - 01:02 PM

You're not giving the phone number fields names, so they will not be submitted with the form. You should give all of them the same names, for example "phonenumbers[]", with the brackets on the end. In PHP, that will make $_POST['phonenumbers'] an array of the phone numbers that they entered. You can loop through that array to get each phone number and insert it into the table.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1