Solved

dynamic insert

Posted on 2013-10-24
8
208 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 42

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 42

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 42

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

760 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

20 Experts available now in Live!

Get 1:1 Help Now