| Index
» Projekty
» phpMyEdit
» Fórum
» before and after triggers (bug?) |
 |
hlslaughter
Založený: 02.07.2003
Príspevky: 12
Bydlisko: San Diego, CA
Zaslal: 2004-09-30 00:25
Návrat hore
Odpovedať s citátom
|
i have a script that uses a trigger that does some table updates.
until now, i'd been using:
$opts['triggers']['insert']['after'] = 'modify_inventory_quantity.inc';
because i assummed all input would be correct. but now i want to do some additional validation and abort the modifications if i don't get what i want.
so i added my new checks and now use:
$opts['triggers']['insert']['before'] = 'modify_inventory_quantity.inc';
the trigger script is essentiall this
check some stuff
if (all is good) {
return("true");
} else {
return("false");
}
regardless of the return value, phpmyedit fails with an error like:
You have an error in your SQL syntax near ') VALUES ('001','','400','1','','1')' at line 5
that's the phpmyedit query, NOT mine.
if i change the trigger back to an 'after' trigger, it works again. but then it inserts a row into the table that should not go in.
only two possibilities that i can think of:
1) there's a bug in phpmyedit
2) i'm not returning properly
to help debug i wrote this trigger:
<?php
$allIsSwell = 0;
if ( $allIsSwell ) {
print "Swell!";
return("true");
} else {
print "Not so Swell!!";
return("false");
}
?>
in both true and false cases i do *not* get the PME sql error message. and also in both cases, a row gets inserted into the database.
does anyone have a trigger script that **does** prevent rows from being added under certain conditions? is my trigger script not properly written? or is this a bug in PME? (naturally i'm assumming it's my fault :)
thanks
harry
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2004-09-30 09:40
Návrat hore
Odpovedať s citátom
|
try false or 0 (not "false") ("false" is considered a string, which is not 0 so it's true)
|
hlslaughter
Založený: 02.07.2003
Príspevky: 12
Bydlisko: San Diego, CA
Zaslal: 2004-09-30 19:10
Návrat hore
Odpovedať s citátom
|
yes, i figured that out after posting.
'return(true/false)' works in my dummy trigger script:
| Kód: |
<?php
$allIsSwell = false;
print "<h1>$allIsSwell</h1>";
return($allIsSwell);
?>
|
but if i use my real trigger script, there is a mysql error generated from the PME query when my trigger returns true and PME goes ahead and does its insert query. when my trigger returns false, PME does not attempt to execute the query (which solves my initial problem).
this is the error i get:
| Citácia: |
| You have an error in your SQL syntax near ') VALUES ('001','','201','1','','1')' at line 8 |
so it would seem that my trigger is somehow invalidating PME's query. i'm not sure how this could be. i'm setting up my own database handle:
| Kód: |
$dbh = mysql_connect($dbh_host, $dbh_user, $dbh_pass)
or die("Could not connect : " . mysql_error());
mysql_select_db($dbh_db) or die("Could not select database");
|
any suggestions would be appreciated. will post back if i figure it out myself.
Harry
|
hlslaughter
Založený: 02.07.2003
Príspevky: 12
Bydlisko: San Diego, CA
Zaslal: 2004-09-30 21:15
Návrat hore
Odpovedať s citátom
|
I figured it out...
my trigger has the following code:
| Kód: |
$query = "
UPDATE inventory
SET count_allocated = '$new_count_allocated',
count_reserve = '$new_count_reserve',
serial_numbers = '$new_serial_numbers',
last_modified = NOW()
WHERE part_num = '$part_num'
";
mysql_query($query) or die("Query failed : " . mysql_error());
/* Free resultset */
mysql_free_result($result);
/* Closing connection */
mysql_close($mydbh);
return(true);
|
But here is the query that is ultimately sent to mysql if use a before trigger rather than an after trigger:
| Kód: |
UPDATE inventory
SET count_allocated = '-3',
count_reserve = '16',
serial_numbers = '100
102
103
104
105
106
107',
last_modified = NOW()
WHERE part_num = '001'
) VALUES ('001','','101','1','','1')
|
That last line obviously invalidates the query. The reason is a namespace collision. I renamed
to
And the problem went away. I'm not sure if PHP allows you to scope variables. I'm just so used to Perl's explicit privatization and Java's inherent privitazation that I don't think about these things anymore.
I also renamed stuff like $result and $dbh. Other than that, I guess it's just a matter of crossing one's fingers :)
|
|
Odoslať novú tému
Odpovedať na tému
|
|
|
|