0

I have a table temp_inventory_cust with the fields customer_id [int(11)] and first_name [varchar(50)], as well as other fields. I'm trying to run this query by typing it into phyMyAdmin, just to make sure it works before coding it in php:

insert into temp_inventory_cust (`customer_id`,`first_name`) values (34,'fred');

It gives me: #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 '\'fred\')' at line 1.

I'm not seeing the problem and am going crazy.

This query runs just fine:

insert into temp_inventory_cust (`customer_id`,`department_id`) values (34,2); 

(both fields are int fields in this case.

Table structure

cust_id              int(11)     No 
first_name           varchar(50) No
last_name            varchar(50) No 
email_address        varchar(100)No 
phone_number         varchar(25) No 
department_id        int(11)     No 
position_id          int(11)     No 
end_date             date        No 
t_stamp              datetime    No 

Any ideas?

2
  • 1
    can you show the table structure? Commented Feb 20, 2013 at 6:45
  • The error can be that the column departmentId will be foreign key and as it is going null for your first query it will be returning error Commented Feb 20, 2013 at 7:26

4 Answers 4

2

The syntax looks just fine.

I tried the query in Fiddle and the schema was built sucessfully.

Here is the code I used:

CREATE TABLE temp_inventory_cust(
customer_id int(11),
first_name varchar(50)
);

INSERT INTO temp_inventory_cust (customer_id,first_name) VALUES (1,'Ankit');

If you want to include the quotation marks then use the escape sequence () before using the quotation marks like

INSERT INTO temp_inventory_cust (customer_id,first_name) VALUES (1,'\'Ankit\'');

check here :

SqlFiddle

Update:

try using double quotes instead of single :

  INSERT INTO temp_inventory_cust (customer_id,first_name) VALUES (1,"Ankit");
Sign up to request clarification or add additional context in comments.

3 Comments

have you used the (;) at the end of creating a table and the query?
Thanks. I tried creating the table again with sql as you did and then ran the query you gave and it gave the same error. I also tried double quotes, and with and without ; at end.
try inserting only the first_name only like INSERT INTO temp_inventory_cust (first_name) VALUES ("Ankit");
0

it would be better if you can share ur table structure but my fist glance says 'issue must be in table first name filed type', its not accepting string. Cross check your filed type in database.

3 Comments

cust_id int(11) No first_name varchar(50) No last_name varchar(50) No email_address varchar(100) No phone_number varchar(25) No department_id int(11) No position_id int(11) No end_date date No t_stamp datetime No No index defined!
sorry that's messy - not sure how to best show table structure
Field Type Null Default cust_id int(11) No first_name varchar(50) No last_name varchar(50) No email_address varchar(100) No phone_number varchar(25) No department_id int(11) No position_id int(11) No end_date date No t_stamp datetime No
0

if you this query directly in phpmyadmin database then no problem

but if you use php code then you must remove " ' ", and query is:

 insert into temp_inventory_cust (customer_id,department_id) values (34,2);

1 Comment

I wish there were no problem, but I've been going crazy with this for hours. The one that trys to insert strings is the one that doesn't work, not the one with just numeric values.
0

As per your error you want to add ' in your string to escape ' you need to add \'

MySQL server version for the right syntax to use near '\'fred\')' at line 1.

Change query to

 insert into temp_inventory_cust (`customer_id`,`first_name`) values (34,'\'fred\''); 

It will add quotes in the db also so record will be 'fred'

If you dont want to add quotes then use

insert into temp_inventory_cust (`customer_id`,`first_name`) values (34,'fred'); 

Refer http://dev.mysql.com/doc/refman/5.0/en/string-literals.html

5 Comments

I didn't want to add 'fred' to the database, just the simple name itself: fred. I put single quotes in the values because it's a string, like one is supposed to do.
Your edited suggestion is exactly the query that I entered originally, which is my problem.
The error can be that the column departmentId will be foreign key and as it is going null for your first query it will be returning error
I made a simpler table just to eliminate things: table tt: a varchar(12) b varchar(12) insert into tt (a,b) values ('c','d') gives me the same error
@user2089988 Which version of mysql you are using ? Have you started facing this problem suddenly ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.