How do I loop through a form and post to database?

Hi,
I have a form with multiple tables, and I need to post the data into a single database table. Look at attached images.

Here is a code example of how my form is generated:

<?php
			$sql = "SELECT * FROM blocks_table WHERE series='100 Series'"; // 100 SERIES --------------------------------------------------------------------
			$result = mysql_query($sql)or die(mysql_error());
			$series = 100;
			$rowcounter = 0;
			echo '<table class="styled-table">';
			echo '<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
			while($row = mysql_fetch_array($result)){
				$rowcounter++;
				echo '<tr><td>'.$row['type'].'</td>
				<td><input type="text" name="'.$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
				<td><input type="text" name="'.$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
				<td><input type="text" name="'.$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
				<td><input type="text" name="'.$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
				</tr>';
			}
			echo '</table>';
		?>
		</div>
		<div class="left">
		<?php
			$sql = "SELECT * FROM blocks_table WHERE series='150 Series'"; // 150 SERIES --------------------------------------------------------------------
			$result = mysql_query($sql)or die(mysql_error());
			$series = 150;
			$rowcounter = 0;
			echo '<table class="styled-table">';
			echo '<tr><th>150 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
			while($row = mysql_fetch_array($result)){
				$rowcounter++;
				echo '<tr><td>'.$row['type'].'</td>
				<td><input type="text" name="'.$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
				<td><input type="text" name="'.$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
				<td><input type="text" name="'.$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
				<td><input type="text" name="'.$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
				</tr>';
			}
			echo '</table>';
		?>
		</div>	

Inserting the data in the order_table is simple enough, because it is not repeating information. The data from the above tables (there are more than these two), will all be inserted into the same table order_details_table.

How do I loop through these tables, and insert the data in the database table???

thanks

If I understand the question, you want to loop through each series to insert the data in a MySQL table. If so, use arrays. Add square brackets to the input fields names (using heredoc style):

name="{$series}perpallet[{$rowcounter}]"
$rowcounter = 0;
echo '<table class="styled-table">';
echo '<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
while($row = mysql_fetch_array($result)){
    $rowcounter++;
    $output = <<<DATADATA
    <tr><td></td>
    <td><input type="text" name="{$series}perpallet[{$rowcounter}]" id="{$series}perpallet{$rowcounter}" style="width:50px;" value="{$row['per_pallet']}" readonly /></td>
    <td><input type="text" name="{$series}quantity[{$rowcounter}]" id="{$series}quantity{$rowcounter}" style="width:50px;" value="0" onChange="getTotal({$series},{$rowcounter});" /></td>
    <td><input type="text" name="{$series}pallets[{$rowcounter}]" id="{$series}pallets{$rowcounter}" style="width:50px;" value="0" class="pallets" readonly /></td>
    <td><input type="text" name="{$series}extras[{$rowcounter}]" id="{$series}extras{$rowcounter}" style="width:50px;" value="0" class="extras" readonly /></td>
    </tr>
DATADATA;
    echo $output;
}
echo '</table>'; 

Sorry because I’m a newbie in PHP and doing this using the forum and google searches.

So once they are arrays, I just use simple insert statements to insert the data? Don’t I have to loop through the html tables to grab the information? Do I use simple insert for every html table individually?

thanks for your help

After you add the square brackets to the name tag, add

print_r ($_POST);

to the top of your action page. Then fill out a test form and submit it. The print_r() function will show you what the form returns. With that in hand you can plan the sql statements. Basically you’ll be doing the following:

foreach($_POST['name'] AS $key=>$value) {
    $sql = "insert into.....";
    $result = mysql_query($sql) or die("didn't work");
}

Let me know how it goes.

Hi, thanks for your help.
I’m getting an error “Parse error: syntax error, unexpected ‘<<’ (T_SL) in C:\xampp\htdocs\hb_bots\bots\order-form.php on line 170

Do I need to do something for the heredoc style to be picked up? Using XAMPP locally on my machine for this.

appreciate your help

heredoc should work with XAMPP as far as I know. Can you post the code that’s giving you problems? Maybe you have extra spaces after the identifier, that’s a common problem.

$output = <<<DATADATA
<tr><td></td>
<td><input type=“text” name=“{$series}perpallet[{$rowcounter}]” id=“{$series}perpallet{$rowcounter}” style=“width:50px;” value=“{$row[‘per_pallet’]}” readonly /></td>
<td><input type=“text” name=“{$series}quantity[{$rowcounter}]” id=“{$series}quantity{$rowcounter}” style=“width:50px;” value=“0” onChange=“getTotal({$series},{$rowcounter});” /></td>
<td><input type=“text” name=“{$series}pallets[{$rowcounter}]” id=“{$series}pallets{$rowcounter}” style=“width:50px;” value=“0” class=“pallets” readonly /></td>
<td><input type=“text” name=“{$series}extras[{$rowcounter}]” id=“{$series}extras{$rowcounter}” style=“width:50px;” value=“0” class=“extras” readonly /></td>
</tr>
DATADATA;
echo $output;

You have a space after “<<<DATADATA” before the newline. :eek:

Thanks for that. Getting this output:

Array ( [companyname] => [deliveryaddress] => [orderdate] => [deliverydate] => [status] => Pending [checked] => No [loadedby] => [sumpallets] => 5 [sumextras] => 118 [makeup] => 1 [grandtotal] => 6 [submit] => Submit Order [100perpallet] => Array ( [1] => 180 [2] => 240 [3] => 360 [4] => 576 [5] => 144 [6] => 288 [7] => 280 [8] => 560 [9] => 360 [10] => 480 [11] => 720 [12] => 1152 ) [100quantity] => Array ( [1] => 185 [2] => 264 [3] => 368 [4] => 657 [5] => 144 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 ) [100pallets] => Array ( [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 ) [100extras] => Array ( [1] => 5 [2] => 24 [3] => 8 [4] => 81 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 ) [150perpallet1] => 144 [150quantity1] => 0 [150pallets1] => 0 [150extras1] => 0 [150perpallet2] => 160 [150quantity2] => 0 [150pallets2] => 0 [150extras2] => 0 [150perpallet3] => 240 [150quantity3] => 0 [150pallets3] => 0 [150extras3] => 0 [150perpallet4] => 384 [150quantity4] => 0 [150pallets4] => 0 [150extras4] => 0 [150perpallet5] => 144 [150quantity5] => 0 [150pallets5] => 0 [150extras5] => 0 [150perpallet6] => 144 [150quantity6] => 0 [150pallets6] => 0 [150extras6] => 0 [150perpallet7] => 135 [150quantity7] => 0 [150pallets7] => 0 [150extras7] => 0 [150perpallet8] => 120 [150quantity8] => 0 [150pallets8] => 0 [150extras8] => 0 [150perpallet9] => 240 [150quantity9] => 0 [150pallets9] => 0 [150extras9] => 0 [150perpallet10] => 320 [150quantity10] => 0 [150pallets10] => 0 [150extras10] => 0 [150perpallet11] => 480 [150quantity11] => 0 [150pallets11] => 0 [150extras11] => 0 [150perpallet12] => 768 [150quantity12] => 0 [150pallets12] => 0 [150extras12] => 0 [150perpallet13] => 288 [150quantity13] => 0 [150pallets13] => 0 [150extras13] => 0 [200perpallet1] => 108 [200quantity1] => 0 [200pallets1] => 0 [200extras1] => 0 [200perpallet2] => 120 [200quantity2] => 0 [200pallets2] => 0 [200extras2] => 0 [200perpallet3] => 180 [200quantity3] => 0 [200pallets3] => 0 [200extras3] => 0 [200perpallet4] => 288 [200quantity4] => 0 [200pallets4] => 0 [200extras4] => 0 [200perpallet5] => 90 [200quantity5] => 0 [200pallets5] => 0 [200extras5] => 0 [200perpallet6] => 90 [200quantity6] => 0 [200pallets6] => 0 [200extras6] => 0 [200perpallet7] => 108 [200quantity7] => 0 [200pallets7] => 0 [200extras7] => 0 [200perpallet8] => 108 [200quantity8] => 0 [200pallets8] => 0 [200extras8] => 0 [200perpallet9] => 108 [200quantity9] => 0 [200pallets9] => 0 [200extras9] => 0 [200perpallet10] => 260 [200quantity10] => 0 [200pallets10] => 0 [200extras10] => 0 [200perpallet11] => 288 [200quantity11] => 0 [200pallets11] => 0 [200extras11] => 0 [200perpallet12] => 108 [200quantity12] => 0 [200pallets12] => 0 [200extras12] => 0 [200perpallet13] => 108 [200quantity13] => 0 [200pallets13] => 0 [200extras13] => 0 [200perpallet14] => 120 [200quantity14] => 0 [200pallets14] => 0 [200extras14] => 0 [200perpallet15] => 240 [200quantity15] => 0 [200pallets15] => 0 [200extras15] => 0 [200perpallet16] => 108 [200quantity16] => 0 [200pallets16] => 0 [200extras16] => 0 [200perpallet17] => 180 [200quantity17] => 0 [200pallets17] => 0 [200extras17] => 0 [200perpallet18] => 180 [200quantity18] => 0 [200pallets18] => 0 [200extras18] => 0 [200perpallet19] => 240 [200quantity19] => 0 [200pallets19] => 0 [200extras19] => 0 [200perpallet20] => 360 [200quantity20] => 0 [200pallets20] => 0 [200extras20] => 0 [200perpallet21] => 576 [200quantity21] => 0 [200pallets21] => 0 [200extras21] => 0 [200perpallet22] => 180 [200quantity22] => 0 [200pallets22] => 0 [200extras22] => 0 [200perpallet23] => 360 [200quantity23] => 0 [200pallets23] => 0 [200extras23] => 0 [200perpallet24] => 108 [200quantity24] => 0 [200pallets24] => 0 [200extras24] => 0 [300perpallet1] => 72 [300quantity1] => 0 [300pallets1] => 0 [300extras1] => 0 [300perpallet2] => 96 [300quantity2] => 0 [300pallets2] => 0 [300extras2] => 0 [300perpallet3] => 120 [300quantity3] => 0 [300pallets3] => 0 [300extras3] => 0 [300perpallet4] => 72 [300quantity4] => 0 [300pallets4] => 0 [300extras4] => 0 [300perpallet5] => 72 [300quantity5] => 0 [300pallets5] => 0 [300extras5] => 0 [300perpallet6] => 72 [300quantity6] => 0 [300pallets6] => 0 [300extras6] => 0 [300perpallet7] => 144 [300quantity7] => 0 [300pallets7] => 0 [300extras7] => 0 [300perpallet8] => 192 [300quantity8] => 0 [300pallets8] => 0 [300extras8] => 0 [300perpallet9] => 240 [300quantity9] => 0 [300pallets9] => 0 [300extras9] => 0 [900perpallet1] => 108 [900quantity1] => 0 [900pallets1] => 0 [900extras1] => 0 [900perpallet2] => 180 [900quantity2] => 0 [900pallets2] => 0 [900extras2] => 0 [900perpallet3] => 180 [900quantity3] => 0 [900pallets3] => 0 [900extras3] => 0 )

So now I’ll use the code you sent me:

foreach($_POST['name'] AS $key=>$value) {
    $sql = "insert into.....";
    $result = mysql_query($sql) or die("didn't work");
}  

to insert the data into the tables. So $key is what will be inserted into the MySQL table as values? And this piece of code will insert the array as rows in the mysql table?

Just trying to understand it, cause I need to insert some data into one table, and some in another.

thanks

The array data you posted does not look like what I expected you’d get in the $_POST variable. How did you print it out?

Did you use

print_r ($_POST);

Your print is no help in getting organized. :frowning:

The print should look more like this (a form I’m working on). The cell ‘name’ should be another array, the rest are just simple cells

Array
(
    [to] => boatyard
    [name] => pipo
    [email] => pipo@home.com
    [subject] => Test
    [comment] => Test
    [telephone] => 123456
    [fax] => 654321
    [yacht_name] => Hot Rod
    [flag] => France
    [hull] => Monohull
    [power] => Motorsailer
    [day] => 21
    [month] => 8
    [year] => 2014
    [units] => Meters
    [length] => 24
    [beam] => 8
    [draft] => 2
    [action] => Revisar
)

yes I used the following:

<form name="frmorder" action="insert-order.php" method="post">

for the form post, and the file insert-order.php has the following in it:

<?php
print_r ($_POST); 
?> 

I notice that the print_r statement is printing all the outpost data, even the once not in the <<<DATATA

Here is my entire page before the <<<DATADATA and the brackets you advised me to use:

<?php
include('db.php')
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>HB - Block Order Tracking System</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript">

	function getTotal(srs,idx) { //Calculates the number of pallets needed and the extra blocks left over
		
		var pidx = srs+'perpallet'+idx;
		var qidx = srs+'quantity'+idx;
		var xidx = srs+'pallets'+idx;
		var eidx = srs+'extras'+idx;

		
		if (!document.getElementById(pidx)) { return false; }
		if (!document.getElementById(qidx)) { return false; }
		if (!document.getElementById(xidx)) { return false; }
		if (!document.getElementById(eidx)) { return false; }
		
		var p = document.getElementById(pidx).value;
		var q = document.getElementById(qidx).value;
		
		if ((p == '') || isNaN(p)) { return false; }
		if ((q == '') || isNaN(q)) { return false; }
		
		q = parseInt(q, 10); // parses a string and returns an integer
		
		var total = Math.floor(q / p); //gets total of pallets and rounds down
		var e = Math.abs(total * p - q); //gets extra blocks and converts to possitive integer
		
		total = parseInt(total, 10);
		e = parseInt(e, 10);
		
		document.getElementById(xidx).value = total;
		document.getElementById(eidx).value = e;
		getSumPallets();
		getSumExtras();
	}
	
	function getSumPallets(){

		var items = document.getElementsByClassName("pallets");
		var itemCount = items.length;
		var total = 0;
		for(var i = 0; i < itemCount; i++){
			total = total +  parseInt(items[i].value);
		}
		document.getElementById('sumpallets').value = total;
	}
	function getSumExtras(){

		var items = document.getElementsByClassName("extras");
		var itemCount = items.length;
		var total = 0;
		for(var i = 0; i < itemCount; i++){
			total = total +  parseInt(items[i].value);
		}
		document.getElementById('sumextras').value = total;
	}
	
	function getGrandTotal(){
		var sump = document.getElementById('sumpallets').value;
		var mup = document.getElementById('makeup').value;
		var gtotal = parseInt(sump) + parseInt(mup)
		
		document.getElementById('grandtotal').value =  parseInt(gtotal);
	}
		
</script>
</head>
<body>
<img class="header-img" src="header-bg.png" alt="header" />
<div id="wrap">
	<form name="frmorder" action="insert-order.php" method="post">
		<div class="left">
			<table class="styled-table">
				<tr>
					<th>Company Name</th><th>Delivery Address</th>
				</tr>
				<tr>
					<td>
						<?php
							$query = mysql_query("SELECT company_name FROM client_table ORDER BY company_name");
							echo '<select name="companyname">';
							echo '<option value="" selected="selected"></option>';
							while ($r = mysql_fetch_array($query)) {
								echo '<option value="'.$r['company_name'].'">'.$r['company_name']."</option>";
							}
							echo "</select>";
						?>
					</td>
					<td><input type="text" name="deliveryaddress" value="" /></td>
				</tr>
			</table>
		</div>
		<div class="left">
			<table class="styled-table">
				<tr>
					<th>Order Date</th><th>Delivery Date</th>
				</tr>
				<tr>
					<td><input type="date" name="orderdate" value="" /></td>
					<td><input type="date" name="deliverydate" value="" /></td>
				</tr>
			</table>
		</div>

				<div class="left">
			<table class="styled-table">
			<tr>
				<th>Status</th><th>Checked</th><th>Loaded by</th>
			</tr>
			<tr>
				<td>
					<input type="radio" name="status" value="Pending" checked>Pending<br>
					<input type="radio" name="status" value="Completed">Completed
				</td>
				<td>
					<input type="radio" name="checked" value="Yes">Yes
					<input type="radio" name="checked" value="No" checked>No
				</td>
				<td>
					<input type="text" name="loadedby" value="" />
				</td>
			</tr>
			</table>
		</div>
		<div class="left clear">
			<table class="styled-table">
			<tr>
				<th>Pallets</th><td><input type="text" name="sumpallets" id="sumpallets" value="" readonly /></td>	
			</tr>
			<tr>
				<th>Extras</th><td><input type="text" name="sumextras" id="sumextras" value="" readonly /></td>	
			</tr>
			<tr>
				<th>Makeup</th><td><input type="text" name="makeup" id="makeup" value="" onChange="getGrandTotal();" /></td>	
			</tr>
			<tr>
				<th>Total Pallets</th><td><input type="text" name="grandtotal" id="grandtotal" value="" readonly /></td>	
			</tr>
			</table>
		</div>
		<div class="left clear" style="margin-top: 15px;">
			<table>
				<tr>
					<td><input type="submit" name="submit" value="Submit Order" /></td>
					<td><input type="button" value="Reset Form" onClick="this.form.reset()" /></td>
				</tr>
				<tr>
					<td colspan="2" style="text-align:center;"><input type="button" name="back" value="Back" onClick="location.href='index.html'" /></td>
				</tr>
			</table>
		</div>
		
		<div class="left clear">
		<?php
			$sql = "SELECT * FROM blocks_table WHERE series='100 Series'"; // 100 SERIES --------------------------------------------------------------------
			$result = mysql_query($sql)or die(mysql_error());
			$series = 100;
			$rowcounter = 0; 
			echo '<table class="styled-table">';
			echo '<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
			while($row = mysql_fetch_array($result)){
				$rowcounter++;
				echo '<tr><td>'.$row['type'].'</td>
				<td><input type="text" name="'.$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
				<td><input type="text" name="'.$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
				<td><input type="text" name="'.$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
				<td><input type="text" name="'.$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
				</tr>';
			}
			echo '</table>';
		?>
		</div>
		<div class="left">
		<?php
			$sql = "SELECT * FROM blocks_table WHERE series='150 Series'"; // 150 SERIES --------------------------------------------------------------------
			$result = mysql_query($sql)or die(mysql_error());
			$series = 150;
			$rowcounter = 0;
			echo '<table class="styled-table">';
			echo '<tr><th>150 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
			while($row = mysql_fetch_array($result)){
				$rowcounter++;
				echo '<tr><td>'.$row['type'].'</td>
				<td><input type="text" name="'.$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
				<td><input type="text" name="'.$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
				<td><input type="text" name="'.$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
				<td><input type="text" name="'.$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
				</tr>';
			}
			echo '</table>';
		?>
		</div>	
		<div class="left">
		<?php
			$sql = "SELECT * FROM blocks_table WHERE series='200 Series'"; // 200 SERIES --------------------------------------------------------------------
			$result = mysql_query($sql)or die(mysql_error());
			$series = 200;
			$rowcounter = 0;
			echo '<table class="styled-table">';
			echo '<tr><th>200 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
			while($row = mysql_fetch_array($result)){
				$rowcounter++;
				echo '<tr><td>'.$row['type'].'</td>
				<td><input type="text" name="'.$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
				<td><input type="text" name="'.$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
				<td><input type="text" name="'.$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
				<td><input type="text" name="'.$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly  /></td>
				</tr>';
			}
			echo '</table>';
		?>
		</div>	
		<div class="left">
		<?php
			$sql = "SELECT * FROM blocks_table WHERE series='300 Series'"; // 300 SERIES --------------------------------------------------------------------
			$result = mysql_query($sql)or die(mysql_error());
			$series = 300;
			$rowcounter = 0;
			echo '<table class="styled-table">';
			echo '<tr><th>300 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
			while($row = mysql_fetch_array($result)){
				$rowcounter++;
				echo '<tr><td>'.$row['type'].'</td>
				<td><input type="text" name="'.$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
				<td><input type="text" name="'.$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
				<td><input type="text" name="'.$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
				<td><input type="text" name="'.$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
				</tr>';
			}
			echo '</table>';
		?>
		</div>
		<div class="left">
		<?php
			$sql = "SELECT * FROM blocks_table WHERE series='Split Face'"; // SPLIT FACE SERIES --------------------------------------------------------------------
			$result = mysql_query($sql)or die(mysql_error());
			$series = 900; //used a int instead of string eg. splitface , cause of the function parameter
			$rowcounter = 0;
			echo '<table class="styled-table">';
			echo '<tr><th>Split Face</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
			while($row = mysql_fetch_array($result)){
				$rowcounter++;
				echo '<tr><td>'.$row['type'].'</td>
				<td><input type="text" name="'.$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly/></td>
				<td><input type="text" name="'.$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
				<td><input type="text" name="'.$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0"  class="pallets" readonly /></td>
				<td><input type="text" name="'.$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
				</tr>';
			}
			echo '</table>';
		?>
		</div>

</form>
</div>
</body>
</html>

getting errors when using

foreach($_POST['name'] AS $key=>$value) {
    $sql = "INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
    VALUES
    ('$_POST[companyname]','$_POST[deliveryaddress]','$_POST[orderdate]','$_POST[deliverydate]','$_POST[status]','$_POST[checked]','$_POST[loadedby]','$_POST[sumpallets]','$_POST[sumextras]','$_POST[grandtotal]')";
    $result = mysql_query($sql) or die("didn't work");
}
?>

[B]Notice: Undefined index: name in [B]C:\xampp\htdocs\hb_bots\bots\insert-order.php on line [B]8

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\hb_bots\bots\insert-order.php on line 8[/B][/B][/B]

sorry but i dont see any input type whose name is “name” in your page.
there is no array “name”.
like u have $_POST[‘companyname’],‘$_POST[deliveryaddress]’,'$_POST[orderdate] and others there is not having any $_POST[‘name’].

Confused now
is there another way to get data from this:

<?php
            $sql = "SELECT * FROM blocks_table WHERE series='150 Series'"; // 150 SERIES --------------------------------------------------------------------
            $result = mysql_query($sql)or die(mysql_error());
            $series = 150;
            $rowcounter = 0;
            echo '<table class="styled-table">';
            echo '<tr><th>150 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
            while($row = mysql_fetch_array($result)){
                $rowcounter++;
                echo '<tr><td>'.$row['type'].'</td>
                <td><input type="text" name="'.$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
                <td><input type="text" name="'.$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                <td><input type="text" name="'.$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
                <td><input type="text" name="'.$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
                </tr>';
            }
            echo '</table>';
        ?>

and insert it into a table?

OK now with this php:

<?php
			$sql = "SELECT * FROM blocks_table WHERE series='100 Series'"; // 100 SERIES --------------------------------------------------------------------
			$result = mysql_query($sql)or die(mysql_error());
			$series = 100;
			$rowcounter = 0;
echo '<table class="styled-table">';
echo '<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
while($row = mysql_fetch_array($result)){
    $rowcounter++;
    $output = <<<DATADATA
	<tr><td>{$row["type"]}</td></td>
    <td><input type="text" name="{$series}perpallet[{$rowcounter}]" id="{$series}perpallet{$rowcounter}" style="width:50px;" value="{$row['per_pallet']}" readonly /></td>
    <td><input type="text" name="{$series}quantity[{$rowcounter}]" id="{$series}quantity{$rowcounter}" style="width:50px;" value="0" onChange="getTotal({$series},{$rowcounter});" /></td>
    <td><input type="text" name="{$series}pallets[{$rowcounter}]" id="{$series}pallets{$rowcounter}" style="width:50px;" value="0" class="pallets" readonly /></td>
    <td><input type="text" name="{$series}extras[{$rowcounter}]" id="{$series}extras{$rowcounter}" style="width:50px;" value="0" class="extras" readonly /></td>
    </tr>
DATADATA;
    echo $output;
}
echo '</table>';
		?>

and the form calling another file which has the following:

<?php
print_r ($_POST);
?>


<?php

foreach($_POST AS $key=>$value) {
    $sql = "INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
    VALUES
	('$_POST[companyname]','$_POST[deliveryaddress]','$_POST[orderdate]','$_POST[deliverydate]','$_POST[status]','$_POST[checked]','$_POST[loadedby]','$_POST[sumpallets]','$_POST[sumextras]','$_POST[grandtotal]')";
	$result = mysql_query($sql) or die("didn't work");
}
?>

I get the following. It does not insert the data:

Array ( [companyname] => NACA [deliveryaddress] => Stuart Park [orderdate] => 2013-04-18 [deliverydate] => 2013-05-16 [status] => Completed [checked] => Yes [loadedby] => MANOLIS [sumpallets] => 3 [sumextras] => 624 [makeup] => 2 [grandtotal] => 5 [submit] => Submit Order [100perpallet] => Array ( [1] => 180 [2] => 240 [3] => 360 [4] => 576 [5] => 144 [6] => 288 [7] => 280 [8] => 560 [9] => 360 [10] => 480 [11] => 720 [12] => 1152 ) [100quantity] => Array ( [1] => 654 [2] => 154 [3] => 356 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 ) [100pallets] => Array ( [1] => 3 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 ) [100extras] => Array ( [1] => 114 [2] => 154 [3] => 356 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 ) ) didn't work

with this:

echo '<pre style="text-align: left;">' . print_r($_POST, true) . '</pre>';

I get:

Array
(
    [companyname] => InfoShell
    [deliveryaddress] => Kavlia street
    [orderdate] => 2013-04-18
    [deliverydate] => 2013-05-23
    [status] => Pending
    [checked] => No
    [loadedby] => Kavlifei
    [sumpallets] => 3
    [sumextras] => 187
    [makeup] => 1
    [grandtotal] => 4
    [submit] => Submit Order
    [100perpallet] => Array
        (
            [1] => 180
            [2] => 240
            [3] => 360
            [4] => 576
            [5] => 144
            [6] => 288
            [7] => 280
            [8] => 560
            [9] => 360
            [10] => 480
            [11] => 720
            [12] => 1152
        )

    [100quantity] => Array
        (
            [1] => 354
            [2] => 245
            [3] => 368
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )

    [100pallets] => Array
        (
            [1] => 1
            [2] => 1
            [3] => 1
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )

    [100extras] => Array
        (
            [1] => 174
            [2] => 5
            [3] => 8
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )

)
didn't work

So now trying to insert values to the table with no luck.

foreach($_POST[] AS $key=>$value) {
    $sql="INSERT INTO order_table ('company_name') VALUES ('$_POST[companyname]')";
    $result = mysql_query($sql) or die("didn't work");
}

this is not working. Not sure what to put in foreach($_POST[" in here"]

ok say I do this:

foreach($_POST['100quantity'] AS $key=>$value) {
    $sql="INSERT INTO order_details_table ('block_quantity') VALUES ('$_POST[blockquantity]')";
    $result = mysql_query($sql) or die("didn't work");
}

the value to insert into the table does not have the name blockquantity in the print_r($_POST). So how do I do it? This value is what is in the form.

I can insert the values for these:

Array
(
    [companyname] => InfoShell
    [deliveryaddress] => safasfasf
    [orderdate] => 2013-04-08
    [deliverydate] => 2013-04-23
    [status] => Pending
    [checked] => No
    [loadedby] => afsfsfsf
    [sumpallets] => 2
    [sumextras] => 351
    [makeup] => 2
    [grandtotal] => 4
    [submit] => Submit Order

The problem I have is with looping and inserting the following in a different table:


[100type] => Array
        (
            [1] => 10.01
            [2] => 10.02
            [3] => 10.03
            [4] => 10.04
            [5] => 10.31
            [6] => 10.83
            [7] => 10.702
            [8] => 10.772
            [9] => 10.71
            [10] => 10.72
            [11] => 10.73
            [12] => 10.74
        )

    [100perpallet] => Array
        (
            [1] => 180
            [2] => 240
            [3] => 360
            [4] => 576
            [5] => 144
            [6] => 288
            [7] => 280
            [8] => 560
            [9] => 360
            [10] => 480
            [11] => 720
            [12] => 1152
        )

    [100quantity] => Array
        (
            [1] => 234
            [2] => 324
            [3] => 213
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )

    [100pallets] => Array
        (
            [1] => 1
            [2] => 1
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )

    [100extras] => Array
        (
            [1] => 54
            [2] => 84
            [3] => 213
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )

To insert the values you would need something like this. It takes the values for the first product, then the second one, and so on until it loops through the 12 products

<?php
foreach($_POST['100type'] AS $key=>$value) {
    $sql = "INSERT INTO table
           (field1, field2, field3, field4, field5)
           VALUES ('{$value}',
                   '{$_POST['100perpallet'][$key]}',
                   '{$_POST['100quantity'][$key]}',
                   '{$_POST['100pallets'][$key]}',
                   '{$_POST['100extras'][$key]}'
                  )"
    $result = mysl_query($sql) or die('Could not insert data');
}
?>

must be doing something wrong… keep getting “Could not insert data”.

this is what I’ve tried:


foreach($_POST['100type'] AS $key=>$value) {
    $sql = "INSERT INTO order_details_table
           (block_type,block_quantity)
                      VALUES ('{$value}',
                   '{$_POST['100type'][$key]}',
                   '{$_POST['100quantity'][$key]}'          
                  )";
	$result = mysql_query($sql) or die('Could not insert data');
}

?>

the table I need to enter values in has 4 fields
id
order_id
block_type
block_quantity

Do I need to insert a value for the id and order_id? and insert NULL?