Elxis CMS Forum

Support => General => Topic started by: ROUBOS on April 23, 2013, 05:46:27

Title: What's wrong with my PHP/MySQL code? [SOLVED]
Post by: ROUBOS on April 23, 2013, 05:46:27
Hi, trying to update table data with no luck.

Here is my code:
Code: [Select]
<?php
include(&#39;db.php&#39;); //connect

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

$id $_POST[&#39;id&#39;];
$company_name mysql_real_escape_string(htmlspecialchars($_POST[&#39;companyname&#39;]));
$delivery_address mysql_real_escape_string(htmlspecialchars($_POST[&#39;deliveryaddress&#39;]));
$order_date mysql_real_escape_string(htmlspecialchars($_POST[&#39;orderdate&#39;]));
$delivery_date mysql_real_escape_string(htmlspecialchars($_POST[&#39;deliverydate&#39;]));
$status mysql_real_escape_string(htmlspecialchars($_POST[&#39;status&#39;]));
$checked mysql_real_escape_string(htmlspecialchars($_POST[&#39;checked&#39;]));
$loaded_by mysql_real_escape_string(htmlspecialchars($_POST[&#39;loadedby&#39;]));
$pallets mysql_real_escape_string(htmlspecialchars($_POST[&#39;sumpallets&#39;]));
$extra_blocks mysql_real_escape_string(htmlspecialchars($_POST[&#39;sumextras&#39;]));
$extra_pallets mysql_real_escape_string(htmlspecialchars($_POST[&#39;makeup&#39;]));
$total_pallets mysql_real_escape_string(htmlspecialchars($_POST[&#39;grandtotal&#39;]));

mysql_query("UPDATE order_table SET company_name=&#39;$company_name&#39;, delivery_address=&#39;$delivery_address&#39;, order_date=&#39;$order_date&#39;, delivery_date=&#39;$delivery_date&#39;, status=&#39;$status&#39;, hb_checked=&#39;$checked&#39;, loaded_by=&#39;$loaded_by&#39;, pallets=&#39;$pallets&#39;, extra_blocks=&#39;$extra_blocks&#39;, extra_pallets=&#39;$extra_pallets&#39;, total_pallets=&#39;$total_pallets&#39; WHERE id=&#39;$id&#39;")
or die(
mysql_error()); 

foreach(
$_POST[&#39;100type&#39;] AS $key=>$value) { // UPDATE 100 TYPE
mysql_query("UPDATE order_details_table SET block_quantity=&#39;{$_POST[&#39;100quantity&#39;][$key]}&#39;, block_pallets=&#39;{$_POST[&#39;100pallets&#39;][$key]}&#39;, block_extras=&#39;{$_POST[&#39;100extras&#39;][$key]}&#39; WHERE order_id=&#39;$id&#39;")
or die(mysql_error()); 
}


//redirect back to the view page
//header("
Locationindex.html");

?>


The first SQL statement works fine. It updates the data. Now the second one has to update another table.
Using "foreach" I'm trying to loop and update the data.

Instead of updating the data from post, it updates with "0". Take a look at my post data:
Code: [Select]
Array
(
    [id] => 40
    [companyname] => Vazelos
    [deliveryaddress] => LEOFOROS
    [orderdate] => 2013-04-23
    [deliverydate] => 2013-08-22
    [status] => Pending
    [checked] => No
    [loadedby] => MANOLIS
    [sumpallets] => 5
    [sumextras] => 347
    [makeup] => 2
    [grandtotal] => 7
    [submit] => Update Order
    [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] => 654
            [2] => 458
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )

    [100pallets] => Array
        (
            [1] => 3
            [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] => 114
            [2] => 218
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )


)

when I echo the sql statement I get:
UPDATE order_details_table SET block_quantity='0', block_pallets='0', block_extras='0' WHERE order_id='40'

so the block_quantity is different to the post data....

any thoughts?
Title: Re: What's wrong with my PHP/MySQL code?
Post by: ROUBOS on April 23, 2013, 06:52:36
I did it. It works now.. :)

Code: [Select]
foreach($_POST['100type'] AS $key=>$value) {
$sql = "UPDATE order_details_table
           SET block_quantity = '{$_POST['100quantity'][$key]}',
               block_pallets = '{$_POST['100pallets'][$key]}',
               block_extras = '{$_POST['100extras'][$key]}'
         WHERE order_id = '$id' AND block_type='{$_POST['100type'][$key]}'";
echo $sql;
mysql_query($sql) or die(mysql_error());