Jump to content

POST issue


Digiboy
Go to solution Solved by Digiboy,

Recommended Posts

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>&pound$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"&pound$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"&pound$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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.