Solved

dynamic insert

Posted on 2013-10-24
8
213 Views
Last Modified: 2013-10-29
I want to generate a page which will allow  me to add extra rows AND submit these to a database when a submit button is clicked

Can anyone direct me to a working script / tutorial on this which actually works please
Once I have the principal I can then work things out
0
Comment
Question by:doctorbill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 500 total points
ID: 39596627
Here's the general idea for the database script.

<?php
$dbh = new PDO("mysql:host=localhost;dbname=yourDB", "username", "password");

//prepare the SQL INSERT Statement
$stmt = $dbh->prepare("INSERT INTO yourTable (column1, column2, column3) VALUES (:var1, :var2, :var3)");

//The data to INSERT using named parameters
$data = array (
	'var1' => $_POST['var1'],
	'var2' => $_POST['var2'],
	'var3' => $_POST['var3'],
); 

//execute the script
$stmt->execute($data);
?>

Open in new window

This will insert the values from a form that has been POSTed with fields called var1, var2 and var3:

<form action="yourScript.php" method="post" accept-charset="utf-8">
	<p>Var 1: <input type="text" name="var1"></p>
	<p>Var 2: <input type="text" name="var2"></p>
	<p>Var 3: <input type="text" name="var3"></p>
  
	<p><input type="submit" value="Continue &rarr;"/></p>
</form>

Open in new window

Should get you started :)
0
 

Author Comment

by:doctorbill
ID: 39596640
Yes but I need to be able to add extra rows when on the php page in a dynamic fashion and also allow them to be submitted
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 500 total points
ID: 39596683
OK. Do you mean you want to dynamically add more data fields to the form and then submit them. Do you want to submit each line one at a time (separate forms) or submit the whole form together.
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:doctorbill
ID: 39596685
I want to be able to add n number of fields as required then submit the whole form
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39596771
OK. You're going to need to use jQuery to dynamically add the rows to your form, and then post that to your PHP script. You'll need to pay particular attention to the naming of your form fields so that you can loop through them in the PHP Script. Best-practice here is to name the fields using an array. When you clone the row, you'll need to set the array key to a new ID.

This is your HTML:

<form action="yourScript.php" method="post" accept-charset="utf-8">
	<div id="rows">
		<div id="masterRow" class="row">
			<p>Var 1: <input type="text" name="item[1][var1]"></p>
			<p>Var 2: <input type="text" name="item[1][var2]"></p>
			<p>Var 3: <input type="text" name="item[1][var3]"></p>
		</div>

	</div>
	<p><input type="submit" value="Continue &rarr;"/></p>
	<button id="addRow">Add New Row</button>
</form>

Open in new window

This is your jQuery (for the HEAD of your document):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('#addRow').click(function(e){
		//prevent the Add button from Submitting the form
		e.preventDefault();
		
		//get the next row number so we can name the form fields correctly
		var nextRow = $('.row').length + 1;
		
		//clone the first row
		var newRow = $('#rows .row:first-child').clone();
		
		//rename each <input> element in the new row 
		newRow.find('input').each(function(i, elem) {
			var newName = $(this).attr('name').replace("[1]", "[" + nextRow + "]");
			$(this).attr('name', newName);
		});
		
		//add the new row
		newRow.appendTo("#rows");
	});
});
</script>

Open in new window

And this is the PHP script that you'll POST your form to:

<?php
//Connect to the database
$dbh = new PDO("mysql:host=localhost;dbname=yourDB", "username", "password");

//prepare the query
$stmt = $dbh->prepare("INSERT INTO yourTable (column1, column2, column3) VALUES (:var1, :var2, :var3)");

//loop through the POST array
foreach ($_POST['item'] as $info):
	//prepare the data for the INSERT		
	$data = array(
		'var1' => $info['var1'],
		'var2' => $info['var2'],
		'var3' => $info['var3'],
	);

	//execute the query
	$stmt->execute($data);
endforeach;
?>

Open in new window

0
 

Assisted Solution

by:doctorbill
doctorbill earned 0 total points
ID: 39596808
This looks reeally good - I will try as soon as possible
0
 

Author Closing Comment

by:doctorbill
ID: 39608082
thanks very much - solution
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question