Digiboy Posted September 14, 2013 Share Posted September 14, 2013 Hi guys, I have written this code to insert shopping items in a database but recently when user adds an item, item code is inserted as 0, I have also my php error on but still cant not figure out why and this happens to both add-product and service posts. It inserts everything else correctly except product or service no Any advise or help is appreciated <?php include_once('includes/header.php'); ?> <?php $get_car_reg = mysql_real_escape_string($_GET['car']); $get_car_reg = mb_strtoupper($get_car_reg); $get_invoice = mysql_real_escape_string($_GET['invoice']); if (isset($_POST['delete'])) { $delete_current_item = mysql_real_escape_string($_POST['delete_me']); $delete = mysql_query("DELETE FROM items WHERE id='$delete_current_item'"); header("Location:generate_invoice.php?car=$get_car_reg&invoice=$get_invoice"); } if (isset($_POST['sp'])) { $final_total = mysql_real_escape_string($_POST['final_total']); $final_discount = mysql_real_escape_string($_POST['final_discount']); $final_invoice_sub = mysql_real_escape_string($_POST['final_sub_total']); $final_vat = mysql_real_escape_string($_POST['final_vat']); $final_total = $final_total - $final_discount; $select_current = mysql_query("SELECT * FROM invoices WHERE invoice_no='$get_invoice'"); if (mysql_num_rows($select_current) >= 1) { $update = mysql_query("UPDATE invoices SET invoice_no='$get_invoice', sub_total='$final_invoice_sub', vat='$final_vat', total='$final_total', discount='$final_discount' WHERE invoice_no='$get_invoice'"); } else { $insert = mysql_query("INSERT INTO invoices (invoice_no, sub_total, vat, total, discount) VALUES ('$get_invoice','$final_invoice_sub','$final_vat','$final_total','$final_discount')"); } if ($insert || $update) { header("Location: print.php?car=$get_car_reg&invoice=$get_invoice"); } } ?> <h2>Invoice</h2> <h3>Car Reg: <?php echo"$get_car_reg"; ?></h3> <? if (empty($get_car_reg) || empty($get_invoice)) { header("Location: create-customer.php"); } else { if (isset($_POST['add-product'])) { $item_price = mysql_real_escape_string($_POST['item_price']); $new_product_no = mysql_real_escape_string($_POST['product_no']); if (empty($new_product_no) || $new_product_no = '0') { echo"<div class='alert alert-error'>You need to select an item</div>"; } else { $insert = mysql_query("INSERT INTO items (invoice_no, item_no, item_type, price) VALUES ('$get_invoice','$new_product_no','Product','$item_price')"); echo"$new_product_no is"; // header("Location:generate_invoice.php?car=$get_car_reg&invoice=$get_invoice"); } } else { if (isset($_POST['add-service'])) { $item_price = mysql_real_escape_string($_POST['item_price']); $new_service_no = mysql_real_escape_string($_POST['service']); if (empty($new_service_no) || $new_service_no = '0') { echo"<div class='alert alert-error'>You need to select an item</div>"; } else { $insert = mysql_query("INSERT INTO items (invoice_no, item_no, item_type, price) VALUES ('$get_invoice','$new_service_no','Service', '$item_price')"); header("Location:generate_invoice.php?car=$get_car_reg&invoice=$get_invoice"); } } } } ///$query="SELECT sum(price) FROM Fuel"; ?> <div class="left-column"> <form class="form" action="" method='POST'> <div class="control-group"> <label>Select Service</label> <div class="controls"> <select name="service"> <option value="0">Select one</option> <?php $select_services = mysql_query("SELECT * FROM services"); while ($row = mysql_fetch_array($select_services)) { $service_name = $row['service_name']; $service_price = $row['service_price']; $service_no = $row['service_no']; echo"<option value='$service_no'>$service_name</option>"; } ?> </select></div></div> <input type="hidden" name="item_price" value="<?php echo"$service_price"; ?>"> <button type="submit" name="add-service" class="btn btn-primary">Add</button> </form> <hr/> <form class="form" action="" method="post"> <div class="control-group"> <label>Select Products</label> <div class="controls"> <select name="product_no"> <option value="0">Select one</option> <?php $select_products = mysql_query("SELECT * FROM products"); while ($row = mysql_fetch_array($select_products)) { $product_sku = $row['sku']; $product_price = $row['price']; $product_name = $row['product_name']; echo"<option value='$product_sku'>$product_name</option>"; } ?> </select></div></div> <input type="hidden" name="item_price" value="<?php echo"$product_price"; ?>"> <button type="submit" name="add-product" class="btn btn-primary">Add</button> </form> <hr/> </div> <div class="right-column"> <table class="table"> <thead> <tr> <th>#</th> <th>Item Type</th> <th>Item Name</th> <th>Item No</th> <th>Price</th> <th>Remove</th> </tr> </thead> <tbody> <tr> <?php $select = mysql_query("SELECT * FROM items WHERE invoice_no='$get_invoice'"); while ($row = mysql_fetch_array($select)) { $current_item_id = $row['id']; $current_item_type = $row['item_type']; $current_item_no = $row['item_no']; $current_item_price = $row['price']; $select_item_name = mysql_query("SELECT * FROM services WHERE service_no='$current_item_no'"); if (mysql_num_rows($select_item_name) == 1) { while ($row = mysql_fetch_array($select_item_name)) { $current_item_name = $row['service_name']; } } /// else { $select_item_name = mysql_query("SELECT * FROM products WHERE sku='$current_item_no'"); if (mysql_num_rows($select_item_name) == 1) { while ($row = mysql_fetch_array($select_item_name)) { $current_item_name = $row['product_name']; } } /// } echo" <tr> <td>$current_item_id</td> <td>$current_item_type</td> <td>$current_item_name</td> <td>$current_item_no</td> <td>£$current_item_price</td> <td><form method='post' action=''><input type='hidden' name='delete_me' value='$current_item_id'><input type='submit' class='btn btn-danger' name='delete' value='Delete'></form></td> </tr> "; } ?> </tbody> </table> <form method="post" action=""> <table class="table"> <thead> <tr> <th>Discount</th> <th>Subtotal</th> <th>Total</th> </tr> </thead> <tbody> <tr> <td><input type="text" name='final_discount' placeholder='2.99' class='input input-mini'/></td> <td><?php $subtotal = mysql_query("SELECT sum(price) FROM items WHERE invoice_no='$get_invoice'"); $invoice_sub = mysql_fetch_array($subtotal); echo"£$invoice_sub[0]"; ?> </td> <td><?php $vat_q = mysql_query("SELECT * FROM company_config WHERE id='1'"); while ($row = mysql_fetch_array($vat_q)) { $vat = $row['vat_percentage']; } $total_vat = $vat * $invoice_sub[0] / 100; $total = $total_vat + $invoice_sub[0]; echo"£$total"; ?></td> </tr> </tbody> </table> <input type='hidden' name='final_total' value='<?php echo"$total"; ?>'> <input type='hidden' name='final_sub_total' value='<?php echo"$invoice_sub[0]"; ?>'> <input type='hidden' name='final_vat' value='<?php echo"floor($total_vat)"; ?>'> <div class='btn-group'> <button class='btn btn-primary' name='sp' type='submit'>Save & Print</button> </div> </form> </div> <?php include_once('includes/footer.php'); ?> Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/ Share on other sites More sharing options...
jcbones Posted September 14, 2013 Share Posted September 14, 2013 Check all of your super global variables ($_GET, $_POST, $_SESSION, $_COOKIE) to make sure they exist. Then check to make sure that your database column type and size allow the values you are passing. Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/#findComment-1449479 Share on other sites More sharing options...
Barand Posted September 14, 2013 Share Posted September 14, 2013 Also - you should put "exit;" after your header() call to stop the rest of the script executing. - instead of SELECT then INSERT or UPDATE (if exists) you can use a single query provided invoice_no is defined as unique INSERT INTO invoices (invoice_no, foo) VALUES ($invoice_no, $bar) ON DUPLICATE KEY UPDATE foo = $bar Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/#findComment-1449482 Share on other sites More sharing options...
Digiboy Posted September 14, 2013 Author Share Posted September 14, 2013 Thank you Also - you should put "exit;" after your header() call to stop the rest of the script executing. - instead of SELECT then INSERT or UPDATE (if exists) you can use a single query provided invoice_no is defined as unique INSERT INTO invoices (invoice_no, foo) VALUES ($invoice_no, $bar) ON DUPLICATE KEY UPDATE foo = $bar If I put exit; it doesnt execute rest of page and about the query, I need to have seperate forms so call to action only happens to one of them forms at same time Check all of your super global variables ($_GET, $_POST, $_SESSION, $_COOKIE) to make sure they exist. Then check to make sure that your database column type and size allow the values you are passing. Checked all, i dont have any cookies, and database column is varchar 200 Main issue is not php becuase when I echo $new_product_no = mysql_real_escape_string($_POST['product_no']); echo"$new_product_no"; My echo result is 0, it passes it as zero to insert query Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/#findComment-1449483 Share on other sites More sharing options...
jcbones Posted September 14, 2013 Share Posted September 14, 2013 Check your POSTed variables. <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/#findComment-1449490 Share on other sites More sharing options...
Digiboy Posted September 15, 2013 Author Share Posted September 15, 2013 this is what I get Array ( [service] => 847153 [item_price] => 4.00 [add-service] => ) Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/#findComment-1449585 Share on other sites More sharing options...
Barand Posted September 15, 2013 Share Posted September 15, 2013 If you haven't noticed, there is no sign of $_POST['product_no'] in that array. Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/#findComment-1449587 Share on other sites More sharing options...
Digiboy Posted September 15, 2013 Author Share Posted September 15, 2013 I added this to add-services form not product; this is for services (They both have same issue) Array ( [product_no] => 951554 [item_price] => 55.00 [add-product] => ) Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/#findComment-1449588 Share on other sites More sharing options...
Digiboy Posted September 15, 2013 Author Share Posted September 15, 2013 found it, it was $new_service_no = '0') it was changing value to 0 so my validation was wrong. Thank you all again. Link to comment https://forums.phpfreaks.com/topic/282150-post-issue/#findComment-1449591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.