Use on_update to sum fields in the same record of detail grid

QuestionsUse on_update to sum fields in the same record of detail grid
Steve Christiansen asked 10 years ago

I am probably using the incorrect syntax for this, but I'm trying to to update the value in a column (dep_total) of a detail grid when an update occurs in a specified row:

$e["on_update"] = array("update_deptotal", null, true);

$g->set_events($e);

function update_deptotal($data)
{
$str = "UPDATE tour_pay SET dep_total = {$data["dep1"]}+{$data["dep2"]}+{$data["dep3"]}+{$data["dep4"]}
WHERE id = {$data["id"]}";
mysql_query($str);
}

These are the posted params (the id, first col, value of the detail grid is correct:
Array
(
[id] => 4
[params] => Array
(
[fname] => Paul
[lname] => Baumgarten
[hotel] => 0.00
[bus] => 0.00
[est_cost] => 0.00
[dep1] => 50.00
[dep1_nbr] => 0
[dep2] => 0.00
[dep2_nbr] => 0
[dep3] => 0.00
[dep3_nbr] => 0
[dep4] => 0.00
[dep4_nbr] => 0
[dep_total] =>
)

)
{"id":"'4'","success":true}

3 Answers
Abu Ghufran answered 10 years ago

By looking at array, $data["id"] is fine. All rest param of query need correction.

$data["params"]["dep1"], $data["params"]["dep2"], $data["params"]["dep3"] and $data["params"]["dep4"].

The ["params"] key is missing in your query.

Steve Christiansen answered 10 years ago

Thanks, I have modified as shown below:

$e["on_update"] = array("update_deptotal", null, true);
$g->set_events($e);
function update_deptotal($data)
{
$str = "UPDATE tour_pay SET dep_total = '{$data["params"]["dep1"]} + {$data["params"]["dep2"]} + {$data["params"]["dep3"]} + {$data["params"]["dep4"]}' WHERE id = {$data["id"]}";

mysql_query($str);
}

The dep_total is not being updated.

The returned array:
Array
(
[id] => 4
[params] => Array
(
[fname] => Paul
[lname] => Baumgarten
[hotel] => 0.00
[bus] => 0.00
[est_cost] => 0.00
[dep1] => 50.00
[dep1_nbr] => 0
[dep2] => 0.00
[dep2_nbr] => 0
[dep3] => 0.00
[dep3_nbr] => 0
[dep4] => 0.00
[dep4_nbr] => 0
[dep_total] =>
)

)
{"id":"'4'","success":true}

Abu Ghufran answered 10 years ago

In your event handler, add echo statement as mentioned below, and check the firebug ajax response when you update grid.
This would show exact query and reason for not updating.

function update_deptotal($data)
{
$str = "UPDATE tour_pay SET dep_total = '{$data["params"]["dep1"]} + {$data["params"]["dep2"]} + {$data["params"]["dep3"]} + {$data["params"]["dep4"]}' WHERE id = {$data["id"]}";

echo $str;

mysql_query($str);

echo mysql_error();

}

Your Answer

16 + 19 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?