Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm not a newbie to PHP but I have encountered a [seemingly] simple problem which I cannot figure out how to resolve.

MySQL throws error that the syntax is wrong.

My Statement is this:

if($value){
        $query = "UPDATE ".$preuploads." SET words = '$words_amount' WHERE id= $sn_id";
        $db->sql_query( $query ) or die( mysql_error() );
    }

And then $words_amount is an integer, $sn_id is also an integer. They are double checked.

The statement when printed before execution is as follows:

UPDATE  SET uploads words = '250' WHERE id= 8081
// edited, with the name of table added since the problem primarily was 
// with the encapsulation and the name of table just was dropped in this question
// and not in the app

however words value ('250') is tested with integer data-type as well, but no change occurs and the error lingers on.

And the error thrown is:

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 'SET words = '250' WHERE id= 8081' at line 1
share|improve this question
1  
remove the string encapsulation on $words_amount? –  Ohgodwhy Jul 5 at 5:32
    
I have already mentioned in the question, it is tested but nothing changes. –  Mostafa Talebi Jul 5 at 5:33
1  
where are you updating your values. cannot see the table in your printed query? and try to run this query directly first in your phpmyadmin. –  Shail Paras Jul 5 at 5:35
    
@Shail Paras You are right the table is not in the printed query but it actually exists and that was my mistake to miss it in the copy&Paste –  Mostafa Talebi Jul 5 at 5:41

4 Answers 4

If I understand your question (and preuploads is a table), then

$query = "UPDATE ".$preuploads." SET words = '$words_amount' WHERE id= $sn_id";

should be

$query = "UPDATE ".$preuploads." SET words = '".$words_amount."' WHERE id=".$sn_id;

Or, even better prepare and use bind_param,

$stmt = $mysqli->prepare("UPDATE ? SET words=? WHERE id=?");
$stmt->bind_param($preuploads, $words_amount, $snd_id);
$stmt->execute();
share|improve this answer

check your string ($words_amount) has any single quotes ' if it is then remove it by using this option on php $words_amount=string_replace("'","/'",$your_string_variable);

share|improve this answer

I have found two errors:

First, not encapsulation of the data should occur, thus:

$words_count should be left as is, not to be encapsulated with '

And the table and fields name should be encapsulated with backtick

share|improve this answer

I think your having problem with name of table. The syntax for update query is

UPDATE table_name SET words = '250' WHERE id= 8081
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.