Jeremy Tyler
Jeremy Tyler

Reputation: 39

How to fix syntax error with single and double quotes

I am new to php and cannot seem to find the error in what I have. I am trying to update a database with a number. I am using an exact replica of what works in another row of the same table. However, I keep getting an error that says there is a syntax error near 'order=IF(LENGTH('3')=0, order, '3') WHERE id='1" at line 1'. Notice that there is a single quote then double quote after id='1". Is there something wrong in my code?

if(isset($_POST['nso']))
{
$nso=$_POST['nso'];
$id=$_POST['id'];
$sql="UPDATE series SET order=IF(LENGTH('$nso')=0, order, '$nso') WHERE id='$id'";
$response=mysql_query($sql) or die("Not able to update." .mysql_error());
echo "<meta http-equiv='refresh' content='0;url=DBE.php'>";
}

Upvotes: 0

Views: 185

Answers (1)

Nebril
Nebril

Reputation: 888

You can try putting order in backticks because it is a keyword in mysql.

UPDATE series SET `order`=IF(LENGTH('$nso')=0, `order`, '$nso') WHERE id='$id'

If this doesn't work you can try removing apostrophes from around values of columns with (most probably) numerical values:

id:

UPDATE series SET `order`=IF(LENGTH('$nso')=0, `order`, '$nso') WHERE id=$id

order:

UPDATE series SET `order`=IF(LENGTH('$nso')=0, `order`, $nso) WHERE id='$id'

or both:

UPDATE series SET `order`=IF(LENGTH('$nso')=0, `order`, $nso) WHERE id=$id

Upvotes: 1

Related Questions