Rifki Rifki - 7 months ago 11
SQL Question

INSERT INTO php MyAdmin

if i have a table as such:

CREATE TABLE IF NOT EXISTS users (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
username varchar(255) COLLATE utf8_unicode_ci NOT NULL,
password varchar(255) COLLATE utf8_unicode_ci NOT NULL,
email varchar(255) COLLATE utf8_unicode_ci NOT NULL,
phone varchar(255) COLLATE utf8_unicode_ci NOT NULL,
name varchar(255) COLLATE utf8_unicode_ci NOT NULL,
created_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
updated_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;


and I try to insert a row into it using phpmyadmin with:

INSERT INTO users ( 'john', 'johndoe', 'johndoe@gmail.com' , '123456', 'John', 2013-06-07 08:13:28, 2013-06-07 08:13:28)


Then why do I get the 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 ''john', 'johndoe', 'johndoe@gmail.com' , '123456', 'John', 2013-06-07 08:13:28, ' at line 1

Answer

The dates need to be quoted as well, and you forgot the VALUES keyword.

Also, because you are not inserting values for every column, you need to specify which columns you are inserting in. So your statement would become:

INSERT INTO users(
  username,
  password,
  email,
  phone,
  name,
  created_at,
  updated_at)
VALUES ( 
  'john', 
  'johndoe', 
  'johndoe@gmail.com' , 
  '123456', 
  'John', 
  '2013-06-07 08:13:28', 
  '2013-06-07 08:13:28')

The proof is in this fiddle: http://sqlfiddle.com/#!2/c35d9/1

Comments