[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

dynamic insert

Posted on 2013-10-24
8
Medium Priority
?
216 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 44

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 44

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
Technology Partners: 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 44

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 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