Getting MySQL Error #1064 when trying to insert data - mysql

I'm trying to follow a tutorial on this website, but when I try to insert the below statement I get the following error:
#1064 - 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
'`INSERT INTO login_admin (user_name, user_pass)
VALUES
(
‘admin’, SHA(‘a`' at line 7
INSERT INTO login_admin (user_name, user_pass)
VALUES
(
‘swashata’, SHA(‘swashata’)
)
INSERT INTO login_admin (user_name, user_pass)
VALUES
(
‘admin’, SHA(‘admin’)
)

You're trying to run two queries at once, not one. Each INSERT statement is a separate query. You have to separate queries with a delimiter (;).
In fact, unless you have some bad settings on your server (allowing multiple queries at once), you can't even do that and need to run them as two separate queries, or combine them using multiple VALUES lists.
Also, please note that you cannot use tick marks (‘) to indicate a string value. You need to use single quotes (') instead.
The best approach is to rewrite your queries as:
INSERT INTO login_admin (`user_name`, `user_pass`) VALUES
( 'swashata', SHA('swashata') ),
( 'admin', SHA('admin') )
Finally, I really hope you aren't using SHA to generate passwords. That's not secure. Use PHP's built-in password functionality.

Related

MySQL "INSERT INTO position" fails

I am trying to insert data in a table but getting error 1064:
INSERT INTO position(positioncode,description)
VALUES ('5000', 'President');
The error message says:
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 'position(positioncode,description) VALUES ('5000',
'President')' at line 1
I have also insert data in other tables on the same way. Can someone help?
position is the name of a function. Quote it by enclosing it inside backticks:
INSERT INTO `position` (positioncode, description) VALUES ('5000', 'President');
The exact behavior of function name parsing is described here:
Function Name Parsing and Resolution.
The description seems to suggest that:
CREATE TABLE count (i INT) could be an error or not depending on IGNORE SPACE setting
CREATE TABLE count(i INT) is always an error
So instead of guessing, always quote built-in function names.

Mysql wont insert into database

As far as I can see my SQL code is formatted correctly, it seems to just be refusing to insert into the database.
this is my code:
INSERT INTO `writings`(`cover`, `pages`) VALUES(['test'], [10]);
i also tried
INSERT INTO `writings`(cover, pages) VALUES(['test'], [10]);
&
INSERT INTO `writings`(cover, pages) VALUES('test', 10);
I encounter this error "#1064 - 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 '['test'], [10])' at line 1 "
table name is correct, aswell as column names.
Any help would be fantastic! :)
As already pointed out on the comment
MySQL does not use [] around values
So you should try this way
INSERT INTO `writings`(`cover`, `pages`) VALUES('test', 10);
OR if you want the cover and pages value as an string of array notation
INSERT INTO `writings`(`cover`, `pages`) VALUES("['test']", "[10]");
First two queries are incorrect as mentioned above.
Third query is absolutely correct and must work. If it doesn't, try using INSERT ... SET syntax:
INSERT INTO `writings` SET cover = 'test', pages = 10;
INSERT INTO writings(cover, pages) VALUES('test', 10);
This is worked for inserting data in mysql. Basic syntax problem in your query, nothing else. Make sure table name and field name is proper match with database & values are of same datatype you mention while created table.
Please try like this :
Sql Query:
INSERT INTO writings (cover, pages) VALUES ('test', 10);

How To resolve Syntax Error when using SQL Insert Into Select MySQL

I Have problem when using INSERT INTO SELECT function on mysql database, error is like this
the right syntax to use near 'INSERT INTO tmpProgkeg SELECT A.Tahun, 1, A.Kd_Urusan, A.Kd_Bidang, A.Kd_Un' at line 4
this Is Mysql Query
https://www.db-fiddle.com/f/tfVhWXC25EAQKHdpHv6VLU/0
i need help, thanks before...
Note that before your INSERT statement, you have a CREATE TEMPORARY TABLE statement:
CREATE TEMPORARY TABLE tmpProgkeg (
...
)
INSERT INTO tmpProgkeg SELECT ...
But you don't terminate the CREATE TEMPORARY TABLE statement with the required semicolon (;) character.
Each SQL statement must be terminated. For example:
CREATE TEMPORARY TABLE tmpProgkeg (
...
);
INSERT INTO tmpProgkeg SELECT ...
The requirement to terminate statements is similar to some other programming languages, for example C++, Java, or PHP.
Tip: When MySQL reports syntax errors, it shows you some portion of an SQL statement. It's telling you "I didn't expect the following code at the position you used it." It got confused because it was expecting some other code, the end of code, and it's showing you what it saw instead, following the point where it got confused.
So when you see
the right syntax to use near 'INSERT INTO ...
You know that you should look at your code right before that point, to see what you forgot.

Insert into table using string in MySql

CREATE TABLE IF NOT EXISTS tmp_park_log (
uniqueid VARCHAR (20),
parked_sec INT
) ;
DELETE
FROM
tmp_park_log ;
INSERT INTO tmp_park_log (uniqueid, parked_sec)
SELECT
uniqueid,
SUM(parked_sec)
FROM
park_log
GROUP BY uniqueid ;
This is executing successfully in MySql:
But, when i put this in a string and use Prepared Statement it gives following 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 'DELETE FROM tmp_park_log; INSERT INTO tmp_park_log (uniqueid, parked_sec) SELEC' at line 1
SQL syntax for prepared statements does not support multi-statements (that is, multiple statements within a single string separated by “;” characters).
See here: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
In the first case you arent using one commando instead you use a few commandos and when you put it in a string as a prepared statement you must create one prepared statement for every single commando you want to execute.
Since you didn't attached the Java code I assume you are
trying to d prepare statement for all the text and not to prepare every and execute it.
few hints:
There is no need to prepare the CREATE TABLE statement you can just use create statement and execute it (as no bind variables are used) http://docs.oracle.com/javase/tutorial/jdbc/basics/tables.html
If your application allows it , consider the use of truncate instead of delete, it will be faster and reduce the log generation.
Don't forget to commit :).
BR

SQL syntax checker in phpMyAdmin rejects good code?

I'm using the SQL window in phpMyAdmin (3.5.7) to insert rows into a MySql table, but am having problems with the syntax checker. It's running on Windows Server 2003.
If I click the "INSERT" button to generate a template, then overtype the values in the template, there is no problem. However I because I need to populate a large number of rows I have auto-generated INSERT statements using Excel, pasted them into a text editor, and then pasted them into the same phpMyAdmin SQL window. The results are baffling...
The statement below (generated using Excel/text editor/copy&paste) gives an error:
#1064 - 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 ''timeslot' ('slot_no', 'slot_starttime') VALUES (3,'00:30')' at
line 1
INSERT INTO 'timeslot' ('slot_no', 'slot_starttime') VALUES (2,'00:15')
The statement below is fine however (it was constructed by clicking the INSERT button on the SQL window then over-typing the text in the template:
INSERT INTO `timeslot`(`slot_no`, `slot_starttime`) VALUES (2,'00:15')
I have copied and pasted both statements into this post directly from the phpMyAdmin window. They look pretty much the same to me, so I can't understand why the first one fails.
???
If you have pasted the results directly as you say, there is a syntax error.
In MYSQL you cannot enclose field or table names using the apostrophe sign. Rather you've got to use the BACK-QUOTE (on the tilde key)
So the following is WRONG:
INSERT INTO 'timeslot' ('slot_no', 'slot_starttime') VALUES (2,'00:15')
And the folowing is CORRECT:
INSERT INTO `timeslot` (`slot_no`, `slot_starttime`) VALUES (2,'00:15')
And even the following is CORRECT (without the back-quotes):
INSERT INTO timeslot (slot_no, slot_starttime) VALUES (2,'00:15')
The common single quote (') is used only for VALUES in an SQL statement.