Unknown column in field list on insert [duplicate] - php

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I have a table with a primary column "MatchId", followed by many other columns.
Unfortunately, I can't seem to get my insert/update query right: Even if I only want to insert an MatchId (Not auto-increment by the way), I get the error Unknown column in 'field list'...
Here is my query:
INSERT INTO `stats` (`MatchId`) VALUES (`123456`);
How do I insert something in this table without getting this error?

You have the wrong types of quotes around the value. Backticks are used around table and column names. To quote a string, use single or double quotes:
INSERT INTO `stats` (`MatchId`) VALUES ('123456');
If it's an integer, you don't need to quote it at all:
INSERT INTO `stats` (`MatchId`) VALUES (123456);
Putting a value in backticks forces it to be treated as a column name, even though it has the syntax of a number. Backticks are the way that MySQL allows you to use column names that have unusual syntax.

Test it in phpmyadmin the unrecognised field is "123456". Change your SQL and wrap the value in single quotes

Related

mysql where clause resitriction? [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I use below php code to generate a random id number
md5(uniqid(rand(), true)
the type of string it generate is something like this
9a423553ce53c4d7a6199fa9254bfdc5
I use that as an ID in Mysql table then I do a standard select query
SELECT * FROM table WHERE id = 9a423553ce53c4d7a6199fa9254bfdc5
I get this error
Unknown column '9a423553ce53c4d7a6199fa9254bfdc5' in 'where clause'
if I just change the id to a simple number like 1 it works.
Why is this?
Have you tried encapsulating that in quotes? In your query id = 9a423553ce53c4d7a6199fa9254bfdc5 You can compare two columns like id = other_id .. MySQL needs to know how to handle your query.
For clarification, should be: SELECT * FROM table WHERE id = '9a423553ce53c4d7a6199fa9254bfdc5';

Php mysql update query is not working? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
I have a simple update query, but it is not working:
update user set active = 'Y' , delete = 'N' where id = 1;// not working
but if I add a special character which phpmyadmin uses then it is working
update `user` set `active` = 'Y' , `delete` = 'N' where `id` = 1;//its working but its database generated
and there is no difference except the ` special character which is not mandatory.
delete is MySQL reserved keywords.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
By adding backtick, you are telling MySQL that the word enclosed is not a MySQL keyword but, rather a database name, table name or field name.
Thus, any conflict is avoided.
DELETE is a reserved keyword in SQL. Without escaping it with the backtick character you will get a syntax error and the query will not work.

PHP: INSERT Into Database Using MySQL [duplicate]

This question already has answers here:
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Closed 9 years ago.
I'm running this on my website:
$PlaceOrder = " Insert INTO Order VALUES ( '$CustomerID' , '$ItemID' , '$Quantity' , '$Date' ) ";
$result = mysql_query ($PlaceOrder);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
But when I ever I do I keep getting the following error message:
Invalid query: 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 'Order VALUES ( '3' , '1' , '12' , '11/05/2013' )' at line 1
I really have no idea what to do now, I tried specifying the columns but that didn't work either. I am very new to this whole thing and I'm executing this based on whats in the manual.
Let's look at your error message again:
[...] the right syntax to use near'OrderVALUES
The first thing it complains about is Order. That's what you substitute the Table from your question for. So let's assume that's your actual table name.
It's also a reserved word. Enclose reserved words in backticks when used as column or table name identifiers.
Order is Key word Just use another word instead of Order
Are you sure there are only 4 columns and are they in the same order as you have specified in your query?
As you suggested try to add the column names, for example:
$PlaceOrder = "INSERT INTO Table (customer, item, quantity, date) VALUES ('$CustomerID', '$ItemID', '$Quantity', '$Date')";
Does this help you?
First of all, you should always specify the columns since you never know when you will have to add new columns to the table and old queries will start doing messy things if you haven't done that.
Having said that, are you sure those are the correct column data types that match the table's?
Also, make sure the date format is valid, you shoud use Y-m-d. You will find more info here.

What happens if i name a field in a mysql table with "when" [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I created a table with 3 columns
ID (Primary key), when (some varchar value), Created_date(timestamp)
Will naming a field with "When" creates any issue?
I have been querying like update table set table.when='$when' where ID='1'
Please suggest
it's ok as long as you have to use the tableName along with the column name
update `table`
set `table`.when='$when'
where ID='1'
SQLFiddle Demo
otherwise, wrap it with backticks
update `table`
set `when`='$when'
where ID='1'
SQLFiddle Demo
Other Link:
MySQL Reserved Keywords List
if possible don't use names or identifiers which are on the reserved keyword list to avoid problems.

MySQL Query Quotes and such [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL - when to use single quotes, double quotes, and backticks?
Question 1
Why does this work?
"SELECT `id` FROM `table` WHERE x= '".$y."'"
but not this?
"SELECT `id` FROM `table` WHERE 'x' = '".$y."'"
^ ^
Notice the extra single quotes
Question 2
Is it better to do id over `id` (with the weird quotes)?
Or is it because that double quotes make it interpret as a variable?
because the server reads x as a value as it is wrap with single quote. backtick escapes a reserved keyword used within the query, usually it is used to wrap around columnNames and tableNames.
in your query,
SELECT `id` FROM `table` WHERE 'x' = '$y'
x there is not a column but a string value.
for question 2, you can eliminate those backticks around id since it is not a Reserved Keyword, here is a full list of reserved keywords in MySQL
MySQL Reserved Keyword
As a sidenote, the query is vulnerable with SQL Injection. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.
How to prevent SQL injection in PHP?

Categories