Hello there, Im currently working on a system that holds invoices and creates them aswell as other things.
Im stumped on something i need help with. I have a area where the invoice that is created needs editing. At the side of the part lines i have a check box that has a check box and when ticked will delete that line once the edited invoice is submitted. Everything else works on the form, it will add new lines and edit lines. but when i click one of the tick boxes and submit it will only remove line 1 of the parts.
I have gone over and over the code and it all looks ok. The data is stored in mysql database
My code
Template file - {parts_list} is replaced with what php works out and shown to user
<table id="mytab" width="100%">
<thead>
<tr>
<th width="166"><a href="#">Part No</a></th>
<th width="402" align="center"><a href="#">Description</a></th>
<th width="71" align="center"><a href="#">Qty</a></th>
<th width="162" align="center"><a href="#">Supplier</a></th>
<th width="134"><a href="#">S. Inv No</a></th>
<th width="127"><a href="#">S. Net</a></th>
<th width="146"><a href="#">Net</a></th>
<th width="5"><a href="#">x</a></th>
</tr>
</thead>
<tbody>
{parts_list}
</tbody>
</table>
<p align="center"> <input type="button" value="Add a new part line" onclick="addRow()"></p>
edit_invoice.php - part of file that works out what the part list is and gen html to insert into the template {parts_list} var
//get parts list
$query_parts = mysql_query("SELECT * FROM invoice_parts WHERE `int_inv_no`='".$result1["inv_no"]."'");
while($row = mysql_fetch_array($query_parts)) {
unset($supplier_list);
$supplier_list .= ' <select name="supplier[]" id="supplier[]">
<option value="0" selected="selected">---------------</option>';
$mysql_supplier_query = mysql_query("SELECT * FROM suppliers");
while($row2 = mysql_fetch_array($mysql_supplier_query)) {
if($row["sid"] == $row2["sid"]) {
$supplier_list .= '<option value="'.$row2["sid"].'" selected="selected">'.$row2["name"].'</option>';
} else {
$supplier_list .= '<option value="'.$row2["sid"].'">'.$row2["name"].'</option>';
}
}
$supplier_list .= '</select>';
$parts_list .= '<tr>
<td><input name="part_no[]" type="text" id="part_no[]" size="5" value="'.$row["part_no"].'" /><input name="pid[]" type="hidden" id="pid[]" value="'.$row["id"].'" /></td>
<td>
<input name="desc[]" type="text" id="desc[]" size="20" value="'.$row["desc"].'" />
</td>
<td><input name="qty[]" type="text" id="qty[]" size="1" value="'.$row["qty"].'" /></td>
<td>'.$supplier_list.'</td>
<td><input name="s_inv[]" type="text" id="s_inv[]" size="10" value="'.$row["sup_inv_no"].'" /></td>
<td><input name="s_net[]" type="text" id="s_net[]" size="2" value="'.$row["sup_net_price"].'" /></td>
<td><input name="net[]" type="text" id="net[]" size="2" value="'.$row["ur_net_price"].'" /></td>
<td><input name="delete[]" type="checkbox" id="delete[]" value="yes" /></td>
</tr>';
}//end while loop
$parts_list .= '<tr>
<td><input name="part_no[]" type="text" id="part_no[]" size="5" /></td>
<td>
<input name="desc[]" type="text" id="desc[]" size="20" />
</td>
<td><input name="qty[]" type="text" id="qty[]" size="1" /></td>
<td>'.$supplier_list2.'</td>
<td><input name="s_inv[]" type="text" id="s_inv[]" size="10" /></td>
<td><input name="s_net[]" type="text" id="s_net[]" size="2" /></td>
<td><input name="net[]" type="text" id="net[]" size="2" /></td>
<td><input name="delete[]" type="checkbox" id="delete[]" value="yes" /></td>
</tr>
';
submit_edit_invoice.php - Takes all info from form from edit_invoice.php and updates the database - This is the bit that deals with the editing and removing of parts
foreach ($_POST["part_no"] as $key => $value) {
//post vars
$pid = $_POST["pid"][$key];
$part_no = $_POST["part_no"][$key];
$desc = $_POST["desc"][$key];
$qty = $_POST["qty"][$key];
$supplier = $_POST["supplier"][$key];
$s_inv = $_POST["s_inv"][$key];
$s_net = $_POST["s_net"][$key];
$net = $_POST["net"][$key];
if ($pid > "0") { // is already in db because it has a pid number
if(isset($_POST["delete"][$key]) && ($_POST["delete"][$key] == "yes")) {
$queryremove_part = mysql_query("DELETE FROM invoice_parts WHERE `id`='".$_POST["pid"][$key]."'");
if(!$queryremove_part) { echo '<p>Error performing query remove invoice parts: ' . mysql_error() . '</p>'; }
} else {
..........................................................................
Every other part of the edit invoice works just the removing of one or more invoice lines. If i do select a line to remove it always does the top line, or i assume its the last line called from the database when its pulled.
Sorry about the long post, iv included everything i can think off so anyone helping has everything to hand
Regards,
Paul