Creating an Edit record form...?

Hi,
I need to create an Edit Record form. I could do it for a simple form, but this form stores the data into two different tables, and it uses loops to generate the tables.

here is the form:

<?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();" class="green-bg" /></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"></div> <!-- Used as a break in the page -->
		<div class="left">
		<?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><input type="text" name="{$series}type[{$rowcounter}]" id="{$series}type[{$rowcounter}]" style="width:75px;" value="{$row["type"]}" readonly</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});" class="green-bg" /></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>';
		?>
		</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++;
    $output = <<<DATADATA
	<tr>
	<td><input type="text" name="{$series}type[{$rowcounter}]" id="{$series}type[{$rowcounter}]" style="width:75px;" value="{$row["type"]}" readonly</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});" class="green-bg" /></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>';
		?>
		</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++;
    $output = <<<DATADATA
	<tr>
	<td><input type="text" name="{$series}type[{$rowcounter}]" id="{$series}type[{$rowcounter}]" style="width:75px;" value="{$row["type"]}" readonly</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});" class="green-bg" /></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>';
		?>
		</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++;
    $output = <<<DATADATA
	<tr>
	<td><input type="text" name="{$series}type[{$rowcounter}]" id="{$series}type[{$rowcounter}]" style="width:75px;" value="{$row["type"]}" readonly</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});" class="green-bg" /></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>';
		?>
		</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; //900 used instead of text Split Face.
			$rowcounter = 0;
			echo '<table class="styled-table">';
			echo '<tr><th>Split Face 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><input type="text" name="{$series}type[{$rowcounter}]" id="{$series}type[{$rowcounter}]" style="width:75px;" value="{$row["type"]}" readonly</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});" class="green-bg" /></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>';
		?>
		</div>

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

And here is the insert script:


<?php
include('db.php'); //connect

	// echo '<pre style="text-align: left;">' . print_r($_POST, true) . '</pre>'; // display POST data
	
mysql_query("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[makeup]','$_POST[grandtotal]')");

$currentid = mysql_insert_id(); // gets the current id to be used as order_id in the order_details_table. this acts ase a join of the two tables. relation.

foreach($_POST['100type'] AS $key=>$value) { // INSERT 100 TYPE
    $sql = "INSERT INTO order_details_table
           (order_id, block_type, block_quantity, block_pallets, block_extras)
			VALUES ('$currentid', '{$_POST['100type'][$key]}','{$_POST['100quantity'][$key]}','{$_POST['100pallets'][$key]}','{$_POST['100extras'][$key]}')";

    $result = mysql_query($sql) or die('Could not insert data');
}

foreach($_POST['150type'] AS $key=>$value) { // INSERT 150 TYPE
    $sql = "INSERT INTO order_details_table
           (order_id, block_type, block_quantity, block_pallets, block_extras)
			VALUES ('$currentid', '{$_POST['150type'][$key]}','{$_POST['150quantity'][$key]}','{$_POST['150pallets'][$key]}','{$_POST['150extras'][$key]}')";

    $result = mysql_query($sql) or die('Could not insert data');
}

foreach($_POST['200type'] AS $key=>$value) { // INSERT 200 TYPE
    $sql = "INSERT INTO order_details_table
           (order_id, block_type, block_quantity, block_pallets, block_extras)
			VALUES ('$currentid', '{$_POST['200type'][$key]}','{$_POST['200quantity'][$key]}','{$_POST['200pallets'][$key]}','{$_POST['200extras'][$key]}')";

    $result = mysql_query($sql) or die('Could not insert data');
}

foreach($_POST['300type'] AS $key=>$value) { // INSERT 300 TYPE
    $sql = "INSERT INTO order_details_table
           (order_id, block_type, block_quantity, block_pallets, block_extras)
			VALUES ('$currentid', '{$_POST['300type'][$key]}','{$_POST['300quantity'][$key]}','{$_POST['300pallets'][$key]}','{$_POST['300extras'][$key]}')";

    $result = mysql_query($sql) or die('Could not insert data');
}

foreach($_POST['900type'] AS $key=>$value) { // INSERT SPLIT FACE TYPE
    $sql = "INSERT INTO order_details_table
           (order_id, block_type, block_quantity, block_pallets, block_extras)
			VALUES ('$currentid', '{$_POST['900type'][$key]}','{$_POST['900quantity'][$key]}','{$_POST['900pallets'][$key]}','{$_POST['900extras'][$key]}')";

    $result = mysql_query($sql) or die('Could not insert data');
}
?>
<html>
<head>
<title>HB - Block Order Tracking System</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<img class="header-img"src="header-bg.png">
	<div id="wrap">
		<div id="centerDiv">
			<input type="button" name="back" value="Back" onClick="location.href='index.html'" />
		</div>
	</div>
</div>
</body>
</html>

Any help would be appreciated. I’m inserting the data in two different tables and need to retrieve it recreating the form to be able to edit and update the records…

thanks in advance

DONE

Please remove this thread