Solved

dynamic insert

Posted on 2013-10-24
8
209 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39597145
0
 

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now