/    Sign up×
Community /Pin to ProfileBookmark

php mysql error for the right syntax to use near

Hi..

I have this code:

[code=php]
$Approved = isset($_POST[‘priority’]);
if ($Approved)
{
$lot_number = $_POST[‘lot_number’];
$sr_number_ = $_POST[‘sr_number_’];
$SubQty = $_POST[‘SubQty’];
$ItemCode = $_POST[‘ItemCode’];
$picked_by = $_POST[‘picked_by’];

$sql = “SELECT stock_item, qty FROM wms WHERE stock_item = ‘$ItemCode’ AND lot_number = ‘$lot_number'”;
$res = mysql_query($sql, $con) or die(mysql_error());

$row = mysql_fetch_assoc($res);

$stock_item = $row[‘stock_item’];
$qty = $row[‘qty’];

if($qty >= $SubQty){

$output = $qty – $SubQty;
$qty_withdraw = ‘0.00’;
}
else{
$output = ‘0.00’;
$qty_withdraw = $SubQty – $qty;
}
}
[/code]

error:

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 ‘MAT-CHE-0040” at line 2

but when I echo the $sql;
the output is:

SELECT stock_item, qty FROM wms WHERE stock_item = ‘MAT-CHE-0040’ AND lot_number = ‘LO120601002’;

and it works.

I don’t know why in php the sql query got an error:

How can I remove that error?

Thank you so much

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@VBAssassinJun 05.2012 — Looks good to me :/ only thing i can think of is the data isn't sanitized:

http://php.net/manual/en/function.mysql-real-escape-string.php

Add that around the $_POST vars and then try. For example if a ' is entered then you will get that error ?

Also, to improve the debugging, replace:
$res = mysql_query($sql, $con) or die(mysql_error());

With this so you can see the offending query:
$res = mysql_query($sql, $con) or die(mysql_error() . " QUERY: SELECT stock_item, qty FROM wms WHERE stock_item = '$ItemCode' AND lot_number = '$lot_number'");

Kind regards,

Scott
×

Success!

Help @newphpcoder spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...