mySQL Query Error at line 1 - php

I wrote this mySQL query and I keep getting an error. Included are the query and the error:
mysql_query("INSERT INTO wp_usermeta(umeta_id, user_id, meta_key, meta_value)
VALUES(NULL, $value, $lastkey, $time())") 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 ')' at line 1
Any help would be greatly appreciated! Thank you.

If your column in 'umeta_id' is default NULL then you don't need to specify it on the insert. 'CURTIME()' is an SQL function that returns current time. Should work if the column 'meta_value' is set to hold only time. I'm assuming you are using PHP. I've found including the variables in tick marks ' works. Also mysql_query is deprecated. You should use mysqli_query(yourDatabaseConnection, yourQuery)
mysql_query("INSERT INTO wp_usermeta (user_id, meta_key, meta_value)
VALUES ('$value', '$lastkey', CURTIME())") or die(mysql_error());

You are passing String thru query to mysql Without putting in Single/Double quotes. Use
mysql_query("INSERT INTO wp_usermeta(umeta_id, user_id, meta_key, meta_value)
VALUES(NULL, $value, '".$lastkey."', '".$time()."')") or die(mysql_error());
this query with string concatenation.

Check type of values was matched with database and umeta_id allow be null .
may be on of field has autoincrement or not null check database again .

you should use NOW()
mysql_query("INSERT INTO wp_usermeta(umeta_id, user_id, meta_key, meta_value)
VALUES(NULL, $value, $lastkey, NOW())") or die(mysql_error());

Related

Query getting executed but shows error

$sql="INSERT INTO $p (q,o1,o2,o3,o4,ta,ma) VALUES ('$q','$o1','$o2','$o3','$o4','$ta','$ma')";
this query is getting executed but shows 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 '(q,o1,o2,o3,o4,ta,ma) VALUES
('','','','','','','')' at line 1
any idea??
use this query
$sql=" INSERT INTO '$p' ( `q` , `o1` ,`o2` ,`o3` , `o4` ,`ta` ,`ma`) VALUES ('$q','$o1','$o2','$o3','$o4','$ta','$ma') "
use single quotes for '$p'
$sql="INSERT INTO '$p' (`q`,`o1`,`o2`,`o3`,`o4`,`ta`,`ma`) VALUES ('$q','$o1','$o2','$o3','$o4','$ta','$ma')";
Always use single comma on php variable if you are using double comma at the start. Like this
$query = "INSERT INTO 'table' WHERE 'user' = '$user'";
in your case:
" INSERT INTO '$p' ( `q` , `o1` ,`o2` ,`o3` , `o4` ,`ta` ,`ma`) VALUES ('$q','$o1','$o2','$o3','$o4','$ta','$ma') "

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'

Having trouble with SQL query

I'm using this query (I changed it):
// SQL query
$squl = "INSERT INTO 'messages' ('id','name' ,'email' ,'subject' ,'content','userid') VALUES ( null,'".$name."', '".$mail."', '".$subject."', '".$content."','');";
// mysql query
$query = mysql_query($squl) or die("message query problem: ". mysql_error());
I get this 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 ''messages' ('id','name' ,'email' ,'subject' ,'content','userid' )VALUES ( null,'' at line 1
What is causing this?
.``) You used a period here instead of a comma so the function is only receiving 5 columns when it needs 6.
Update:
As the commenter below points out, you've replaced the backticks with quotation marks.
$squl="INSERT INTO `messages` (`id`,`name` ,`email` ,`subject` ,`content`,`userid` )VALUES ( null,'$name', '$mail', '$subject', '$content','');";
(id,name ,email ,subject ,content,userid )
( NULL,".$name.", ".$mail.", ".$subject.", ".$content."**.**``);
you are using '.' instead of ,
Well, that's about the clearest message you get from SQL. You try to insert 5 values into 6 columns.
The problem that there's no comma between the last two values. Instead there's a . which makes the parser think it's only one value.
You are trying to insert into 6 columns:
id
name
email
subject
content
userid
But have only specified 5 values:
NULL
$name
$mail
$subject
$content
You've got a dot where you should have a comma:
".$subject."`, `".$content."`.``);";
Change that last dot to a comma and you should be golden
You've got 6 fields in your fields list, but are inserting only 5 values in your values list. Looks like you've got a . instead of a ,:
`, `".$subject."`, `".$content."`.``
^--- here
As well, there is NO reason to use repeated string concatenation as you are. PHP can insert variables into double-quoted strings quiet easily:
$sql = "INSERT INTO .... (...) VALUES (NULL, '$name', '$mail', '$subject', '$content', '')";
Note that the 'null' value is not quoted. Backticks are there to escape reserved words. If you intend to insert a real database null, then use the bare word null. If you want a literal string 'null' to go in, then quote it normally: 'null'.
You have six fields listed the first set of parentheses and only five fields in VALUES. That's what column count means.

mysql "insert into" SQL syntax error problem

this is the php code:
$stringquery = "INSERT INTO sikurim(name, title, desc, category, subcategory)
VALUES ('$contact', '$heading','$comments', '$catF', '$catS' ) ";
mysql_query($stringquery) or die(mysql_error());
And i get the 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 'desc, category, subcategory) VALUES ('jhjh', ' fffff','fffff',
'2', '4' )' at line 1
I can't find what's wrong with the code, can someone help?
DESC is reserved MySQL keyword. You need to enclose it in backquotes:
$stringquery = "INSERT INTO sikurim(name, title, `desc`, category, subcategory) VALUES ('$contact', '$heading','$comments', '$catF', '$catS' )";
You should escape each value.
Use mysql_escape_string http://www.php.net/manual/en/function.mysql-escape-string.php

MYSQL Updating multiple columns using variables

I used this query to insert all my values into this database:
INSERT INTO products ($fields) VALUES ($values)
However, I try to use the same format for UPDATE:
UPDATE products SET ($fields) VALUES ($values) WHERE sku = '$checksku'
...and am getting thrown a syntax 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 '('product,make,model,' at line 1
I can't figure it out. Would appreciate any help. Thanks.
UPDATE syntax is different than INSERT syntax. An example of UPDATE would be:
"UPDATE products SET field1 = 'value1', field2 = '$val2', field3 = 5 WHERE sku = '$checksku'"
Though this may be insecure. You should look into parameterized queries.
INSERT INTO products ($fields) VALUES ($values) ON DUPLICATE KEY UPDATE field = VALUES(field), ...
Don't forgot about unique or primary key
you need an =
UPDATE products SET ($fields) = $values WHERE sku = '$checksku'

Categories