Jump to content

php mysql error for the right syntax to use near


newphpcoder

Recommended Posts

Hi..

 

I have this code:

 

$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;
}   
}

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

The value of $ItemCode = 'MAT-CHE-0040'

 

<td><input type='text' name='ItemCode' id='ItemCode' value='$ItemCode' readonly='readonly' style='border:none;'></td>

 

I also tried this:

 

$sql = "SELECT stock_item, qty FROM wms WHERE stock_item = '" . $ItemCode . "' AND lot_number = '" . $lot_number . "'";   

Still same error :(

 

Thank you

I agree with PFMaBiSmAd:

 

Try this:

 

$Approved = isset($_POST['priority']);
if ($Approved)
{
    $lot_number = trim($_POST['lot_number']);
    $sr_number_ =trim( $_POST['sr_number_']);
    $SubQty = trim($_POST['SubQty']);
    $ItemCode = trim($_POST['ItemCode']);
    $picked_by = trim($_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;
}   
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.