...

View Full Version : multiple rows inserted into table



thelung
02-08-2011, 02:29 AM
Hello, I posted a thread some time ago regarding the same issue. I've written numerous versions and made endless revisions; trying pieces from many similiar source and well, it's still not quite working out for me. I'm still very new to php. But, I've been learning a lot in the last couple weeks :thumbsup: but unfortunetly still have no results. :(

The form I've been trying to finish will have multiple rows. The user will input values specific to each row, but I'm trying to make it so that each row is synonymous with each other. Like an order form, an inventory form, or a roster. I'd really like to make this code work out because I can think of numerous applications for it's use. I'm sure others could use it for more.

So, as you see; the html code represents the table that contains the form. Currently, I only have five of these rows in the form, but would like for anybody using the code to be able to add in code for as many rows as needed.

I'd like to represent the idea like this:
FORM
INPUT 1 INPUT 2 INPUT 3 INPUT 4 INPUT 5
INPUT 1 INPUT 2 INPUT 3 INPUT 4 INPUT 5
INPUT 1 INPUT 2 INPUT 3 INPUT 4 INPUT 5
INPUT 1 INPUT 2 INPUT 3 INPUT 4 INPUT 5
/FORM
All rows of html identical, only the values would differ.
Here's my html so far:

<FORM action="post_multiple.php" method="POST">
<table width="80%" border="1">
<tr>
<td>Vendor</td>
<td>Item Description</td>
<td>Quantity</td>
<td>Price</td>
</tr>
<tr>
<td><select name="vendor[0]" id="vendor[0]" >
<option value="other1">other1</option>
<option value="other2">other1</option>
<option value="other3">other3</option>
</select></td>
<td><input name="description[0]" type="text" id="description" size="50" /></td>
<td><input name="quantity[0]" type="text" id="quantity" size="50" /></td>
<td><input name="price[0]" type="text" id="price" size="50" /></td>
</tr>
<tr>
<td><select name="vendor[1]" id="vendor[]">
<option value="other1">other1</option>
<option value="other2">other1</option>
<option value="other3">other3</option>
</select></td>
<td><input name="description[1]" type="text" id="description" size="50" /></td>
<td><input name="quantity[1]" type="text" id="quantity" size="50" /></td>
<td><input name="price[1]" type="text" id="price" size="50" /></td>
</tr>
<tr>
<td><select name="vendor[2]" id="vendor">
<option value="other1">other1</option>
<option value="other2">other1</option>
<option value="other3">other3</option>
</select></td>
<td><input name="description[2]" type="text" id="description" size="50" /></td>
<td><input name="quantity[2]" type="text" id="quantity" size="50" /></td>
<td><input name="price[2]" type="text" id="price" size="50" /></td>
</tr>
<tr>
<td><select name="vendor[3]" id="vendor">
<option value="other1">other1</option>
<option value="other2">other1</option>
<option value="other3">other3</option>
</select></td>
<td><input name="description[3]" type="text" id="description" size="50" /></td>
<td><input name="quantity[3]" type="text" id="quantity" size="50" /></td>
<td><input name="price[3]" type="text" id="price" size="50" /></td>
</tr>
<tr>
<td><select name="vendor[4]" id="vendor">
<option value="other1">other1</option>
<option value="other2">other1</option>
<option value="other3">other3</option>
</select></td>
<td><input name="description[4]" type="text" id="description" size="50" /></td>
<td><input name="quantity[4]" type="text" id="quantity" size="50" /></td>
<td><input name="price[4]" type="text" id="price" size="50" /></td>
</tr>
<tr>
<td><select name="vendor[5]" id="vendor">
<option value="other1">other1</option>
<option value="other2">other1</option>
<option value="other3">other3</option>
</select></td>
<td><input name="description[5]" type="text" id="description" size="50" /></td>
<td><input name="quantity[5]" type="text" id="quantity" size="50" /></td>
<td><input name="price[5]" type="text" id="price" size="50" /></td>
</tr>
</table>
<input type="submit" name="Submit" value="Submit" />
</FORM>


And here's the PHP file "post_multiple.php" that the html form calls on:

<?php
ini_set('display_errors','On');
error_reporting(E_ALL);

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="testing"; // Database name
$tbl_name="test_multiple"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

//declarations
$vendor[] = $_POST['vendor'];
$description[] = $_POST['description'];
$quantity[] = $_POST['quantity'];
$price[] = $_POST['price'];
$Submit = 'Submit';

// declare your limit for the loop
$limit = count($description);

// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$limit;$i++){
$sql1="INSERT INTO $tbl_name (vendor, description, quantity, price) VALUES ('".$vendor[$i]."', '".$description[$i]."', '".$quantity[$i]."', '".$price[i]."')";
$result1=mysql_query($sql1);
}
}
mysql_close();
?>

This is my most current error message:

Notice: Use of undefined constant i - assumed 'i' in /var/www/multiple/post_multiple.php on line 28 Notice: Undefined index: i in /var/www/multiple/post_multiple.php on line 28

Can anybody lend me some suggestions or tell me if I'm totally on the wrong track?? I'm a total noob and barely have a clue what I'm doing; I'm just trying really hard to learn and really want to make some cool stuff for myself and whoever else may want to use.

I'd also love to have some links to reference materials anybody may think could help for this or any similar kind of application..

Thanks a mil to anybody who reads this, even if you don't reply!!! :thumbsup: I really appreciate it!!

mlseim
02-08-2011, 03:23 AM
Like this?
http://www.drasticdata.nl/DDDrasticGrid.php

add rows, delete rows, edit rows ... AJAX, MySQL, PHP.

thelung
02-08-2011, 06:20 PM
that looks very neat. Is the code easy to learn from? I know almost nothing about AJAX and only very basic Javascript. I'm definetly going to d/l that and play around with it, though. :D

Thanks for sharing the link. It looks like something I'll have fun with. :thumbsup:

To answer your question; yes, like that. Only where the user could fill in several lines at one time, then submit it to the database.

I'm at work right now. I'm going to finish a report, catch up on a couple phone calls, d/l the code for that and play with it for a while.

Thanks a mil buddy!!!

thelung
02-22-2011, 08:17 PM
wow, DrasticTools is some pretty neat stuff.. Thanks for sharing this link to me. For some reason I don't know, I probably would've never found it otherwise..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum