Error when running MySQL query, looking for right syntax - php

I get the following error when running my .php file:
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 ''feeds_list' SET 'cron' = now() WHERE 'feed_id' = '1'' at line 1
Here is the code:
$sql = mysql_query("UPDATE 'feeds_list' SET 'cron' = now() WHERE 'feed_id' = '$feed_id'") or die (mysql_error());
Thanks for helping me out

Remove the '' around table names and column names, it should be:
UPDATE feeds_list SET cron = now() WHERE feed_id = '$feed_id'
or use a backticks (``).

$sql = mysql_query("UPDATE feeds_list SET cron = now() WHERE feed_id = '$feed_id'") or die (mysql_error());
USE cron Not 'cron'

if you are putting single quotes on the field name it will show error, don't get confused in single quotes and backticks they are completely different.

Related

Using mysql user defined variables with php

I have an SQL statement which runs perfectly when used on phpmyadmin,
$q1 = " SET #pos = 0; UPDATE `songs` SET `tweek` = ( #pos:= #pos+1) WHERE `approved` = 1 ORDER BY votes DESC ";
my connection is fine, every other thing is fine but I keep getting an error when I use it in my php code.
mysql_query($q1, $link) or die(mysql_error());
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 'UPDATE `songs` SET `tweek` = 1 WHERE `approved` = 1 ORDER BY
votes DESC' at line 1
Please help.
SET and UPDATE are two queries and you must split it into two different calls of mysql_query

php mySQL error of syntax

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
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();
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);
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
I think your having problem with name of table. The syntax for update query is
UPDATE table_name SET words = '250' WHERE id= 8081

Update query MySQL PHP

im trying to update my table using the following query...
$query = mysql_query("UPDATE `outgoings` (id, user_id, bill, bill_name, bill_description, bill_colour ) VALUES ('$id', '$uid', '$bill', '$billname', '$billdescription', '$billcolour') WHERE id = '$id'") or die(mysql_error());
It returns...
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 '(id, user_id, bill, bill_name, bill_description, bill_colour ) VALUES ('', '8464' at line 1
Ive tried removing ' around my variables and googling for alternative methods but cant seem to figutre out what imdoing wrong?
Use this syntax for update statements:
UPDATE `outgoings` set id = '$id', user_id = '$uid' ... where ...
You got it mixed with insert statement I guess.
It looks like your ID is empty (...VALUES ('',...). Should there be an ID there?
Your $id seems to be empty or not defined yet. Read mysql.error() up to the end.
The update query has different syntax, something like that:
UPDATE `outgoings` SET user_id='$uid', bill='$bill' WHERE id = '$id'

Mysql query with a column name having '%' sign in it not executing in PHP

I have a Mysql query as follows
$query = "UPDATE student_database SET fname='$fname',mname='$mname',lname='$lname',dob='$dob',Age='$age',Sex='$sex',Caste='$caste',dept='$dept', SSC%=$ssc , HSC%=$hsc, ATKTs=$atkt, Last_sem%=$lastsem, Aggregate%=$agg WHERE student_id=$id ; ";
Some column names have a '%' sign in it. Mysql throws the following error when I execute it
Cannot execute.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 '%=79.6 , HSC%=81.83, ATKTs=5, Last_sem%=52.35, Aggregate%=53.6 WHERE student_id=' at line 1
Can't figure out the problem I have tried "\" , "#" , "%%" as escape characters but can't figure it out.
Wrap it in backticks to tell SQL that it's a column, does this work?
$query = "UPDATE student_database SET fname='$fname',mname='$mname',lname='$lname',dob='$dob',Age='$age',Sex='$sex',Caste='$caste',dept='$dept', `SSC%`=$ssc , `HSC%`=$hsc, ATKTs=$atkt, `Last_sem%`=$lastsem, `Aggregate%`=$agg WHERE student_id=$id ; ";
Try wrapping the column names in backticks, e.g
`SSC%`=$ssc

Simple Where Clause has syntax error

$entries = "INSERT INTO allowances (totalGrossPay) VALUES ('".$totalGrossPay."') WHERE (allowances.SSN = '".$SSN."')";
mysql_query ($entries) or die (mysql_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 'WHERE (allowances.SSN = '300497654')'
What do you think should happen? INSERT is unconditional, no WHERE clause is required or even allowed.
What you really want is an UPDATE.
Just do an update:
$entries = "UPDATE `allowances` SET `totalGrossPay` = '{$totalGrossPay}' WHERE `SSN` = '{$SSN}'";
mysql_query ($entries) or die (mysql_error());
And if you want to update a possibly existing record and otherwise insert the record, you can use the ON DUPLICATE KEY phrase of the INSERT statement to specify what happens if the record already exists.

Categories