?
Solved

dynamic insert

Posted on 2013-10-24
8
Medium Priority
?
214 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 2000 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
Suggested Courses

741 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