newphpcoder Posted June 5, 2012 Share Posted June 5, 2012 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 Link to comment https://forums.phpfreaks.com/topic/263680-php-mysql-error-for-the-right-syntax-to-use-near/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 5, 2012 Share Posted June 5, 2012 Your $_POST['ItemCode'] value probably has a new-line character at the start of it. Where is the value in the $_POST['ItemCode'] field being set from? Link to comment https://forums.phpfreaks.com/topic/263680-php-mysql-error-for-the-right-syntax-to-use-near/#findComment-1351294 Share on other sites More sharing options...
newphpcoder Posted June 5, 2012 Author Share Posted June 5, 2012 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 Link to comment https://forums.phpfreaks.com/topic/263680-php-mysql-error-for-the-right-syntax-to-use-near/#findComment-1351298 Share on other sites More sharing options...
PFMaBiSmAd Posted June 5, 2012 Share Posted June 5, 2012 Where is the $itemCode value in the form's value='$ItemCode' attribute coming from? Link to comment https://forums.phpfreaks.com/topic/263680-php-mysql-error-for-the-right-syntax-to-use-near/#findComment-1351511 Share on other sites More sharing options...
harristweed Posted June 6, 2012 Share Posted June 6, 2012 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; } } Link to comment https://forums.phpfreaks.com/topic/263680-php-mysql-error-for-the-right-syntax-to-use-near/#findComment-1351587 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.