Experts Round Table Network

Databases => MySQL => Topic started by: thepreacher on June 20, 2007, 12:51:31 PM



Title: Struggling with this one
Post by: thepreacher on June 20, 2007, 12:51:31 PM
The following is an SQL statement i am struggling with at the moment
Code
Language: sql (GeSHi-highlighted)
$sql1 = "UPDATE custpayments SET depcardbal = depcardbal -". $totcardsubtotal.", projacctbal = projacctbal - ".$totprojsubtotal.", bankacctbal = bankacctbal - ".$bankacctwithdraw.", transfacctbal = transfacctbal - ".$transfacctwithdraw.", transactionstatus = '2' WHERE paytransacid = '".$paytransacid."'";

I get the following error message;

Quote
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' bankacctbal = bankacctbal - , transfacctbal = transfacctbal - , transactionstat' at line 1

Thanks for any help you can provide.


Title: Re: Struggling with this one
Post by: GrandSchtroumpf on June 20, 2007, 04:06:24 PM
Looks like your variables are empty.
You should make sure you initialize them (set them all to 0) when you declare them.

Or maybe try adding a leading zero to your amounts, but that's a quick and dirty fix...

Code:
$sql1 = "UPDATE custpayments
SET
depcardbal = depcardbal - 0". $totcardsubtotal.",
projacctbal = projacctbal - 0".$totprojsubtotal.",
bankacctbal = bankacctbal - 0".$bankacctwithdraw.",
transfacctbal = transfacctbal - 0".$transfacctwithdraw.",
transactionstatus = '2'
WHERE
paytransacid = '".$paytransacid."'";
(i like my SQL statements on multiple lines)

> $paytransacid
You should always pay for your trance acid ;.


Title: Re: Struggling with this one
Post by: VGR on June 21, 2007, 11:23:42 AM
the above is correct.

Moreover, you don't need to play with quotes the way you do, and your SQL phrase is not closed.

IMHO, it should read :
Code:
$sql1 = "UPDATE custpayments SET depcardbal = depcardbal -$totcardsubtotal, projacctbal = projacctbal - $totprojsubtotal, bankacctbal = bankacctbal - $bankacctwithdraw, transfacctbal = transfacctbal - $transfacctwithdraw, transactionstatus = '2' WHERE paytransacid = $paytransacid;";
moreover, your variables are empty. Given they're numeric, they should be initialized explicitly at the start of your program/script (a good habit I inherited from Pascal).
A workaround#1 is to use the 0 trick
A workaround#2 is to add before the query line above :
Code:
$totcardsubtotal=(isset($totcardsubtotal)?(($totcardsubtotal>0)?$totcardsubtotal:0):0); // repeat this line for all variables
$sql1 = "UPDATE custpayments ...
A workaround#3 is to explictly cover the case in your SQL statement, but this is a bit overkill :
Code:
$sql1 = "UPDATE custpayments SET depcardbal = depcardbal - case when $totcardsubtotal is not null then $totcardsubtotal else 0 end,

best regards


Title: Re: Struggling with this one
Post by: thepreacher on June 21, 2007, 11:45:01 AM
Thanks a lot for your help but the variables are actually retrieved from $_POST earlier on so if they are empty them something real strange is happening. Will have to look into that.


Title: Re: Struggling with this one
Post by: VGR on June 22, 2007, 03:10:12 PM
what I wrote is correct, as I usuammy use the same script for offering the FORM and treat the POSTed data.

In your case, always assign a value="0" to your (numeric) form fields. This way, they'll never be empty... Unless the user cleans them up manually, but this should be covered in the javascript client-side form data validation PRIOR to the actual submit. OR use what I said : $somevar=(isset())? stuff with $_POST['somevar'] in stead of $somevar.

like :
Code:
if isset($_POST['submitbuttonname']) {
  $somevar=(isset($_POST['somevar'])?$_POST['somevar']:0;
  // etc
}
// hear continue with POSTed data processing