insert error

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

insert error

Post by bimo »

I am having problems with a silly little mysql insert statement that I hope someone can help me with. Everytime I run this:

Code: Select all

$query3 = "insert into table employee (emp_fname, emp_lname, emp_id, emp_perm, emp_pass, emp_email) values ($emp_fname, $emp_lname, $emp_id, $emp_perm, $emp_pass, $emp_email)";
I get an error like this where it truncates the query
Check the manual that corresponds to your MySQL server version for the right syntax to use near 'table employee (emp_fname, emp_lname, emp_id, emp_perm, emp_pas
at first it seemed to be cutting it where the line was wrapping but I tried turning wordwrap off and it still didn't work. Is this the kind of thing that something like mysql_escape_string would work on?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

as the error says: read the manual :p varchar values need to be between quotes...

Code: Select all

$query3 = "insert into table employee (emp_fname, emp_lname, emp_id, emp_perm, emp_pass, emp_email) values ('$emp_fname', '$emp_lname', '$emp_id', '$emp_perm', '$emp_pass', '$emp_email')";
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

doh...

Post by bimo »

I'm sorry for asking such a simple, simple question.

..thanks
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

Post by bimo »

it looks like I spoke too soon. It seems to do the same thing. Here's the entire error
Array ( ) cannot insert record: insert into table employee (emp_fname, emp_lname, emp_id, emp_perm, emp_pass, emp_email) values ('Qndres', Swift, 'ahws123', '8', 'bdb123', 'd@b.com'). - 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 'table employee (emp_fname, emp_lname, emp_id, emp_perm, em
the code is

Code: Select all

$query3 = "insert into table employee 
			(emp_fname, emp_lname, emp_id, emp_perm, emp_pass, emp_email) 
			values ('$emp_fname', '$emp_lname', '$emp_id', '$emp_perm', '$emp_pass', '$emp_email')"; 
$result3 = mysql_query($query3) or die ("cannot insert record: $query3. - " . mysql_error());
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

The syntax for inserting values into a table is

Code: Select all

INSERT INTO
  table_name (col1, ...)
VALUES
  (val1, ...)
so your code should be

Code: Select all

$query3  = 'INSERT INTO ';
$query3 .=  'employee ';
$query3 .= '(emp_fname, emp_lname, emp_id, emp_perm, emp_pass, emp_email)';
$query3 .= ' VALUES (';
$query3 .= mysql_escape_string($emp_fname);
$query3 .= mysql_escape_string($emp_lname);
$query3 .= mysql_escape_string($emp_id);
$query3 .= mysql_escape_string($emp_perm);
$query3 .= mysql_escape_string($emp_pass);
$query3 .= mysql_escape_string($emp_email);
$query3 .= ')';
$result3 = mysql_query($query3) or die ('cannot insert record: ' . $query3 . ' - ' . mysql_error());
One of the reasons for constructing the query like this, is that is is easy to see if you have forgotten a quote.
Also, any data that you enter into the database should be protected by the mysql_escape_string. This makes sure that if your data contains the quote character, it is correctly escaped.

Hope this helps
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

don't forget that you might have a space at the end of your row's name. (like i did)
So when you created your table be sure there is nospace (it's sometimes hard to pin point)

if you have phpmyadmin , go look at your table and see if ll name have no spaces.
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

Post by bimo »

Thanks for your help. all.
Post Reply