We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,657 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Column 'id' cannot be null

Hi,

I'm getting this error: Column 'id' cannot be null. I can't figure it out why.

I always Import the tables by using .sql into MyPHPAdmin. I only have 2 tables. One is mypost and the other username. For my .sql file I always have the same format with NOT NULL for all the data but this time around it doesn't work. I try to leave it out but I still get Column 'id' cannot be null

The only thing I always fill in is in the username table which is the username and password.

When I open the admin section and try to login this message pops up column 'id' cannot be null and also when I try to post something this message Column 'id' cannot be null pops up too.

I didn't put anything in the id section for mypost and username.

Here is my tables:

CREATE TABLE `mypost` (
  `id` int(15) NOT NULL,
  `title` varchar(150) NOT NULL,
  `content` text NOT NULL,
   PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `username` (
  `id` int(15) NOT NULL,
  `username` varchar(25) NOT NULL,
  `password` varchar(250) NOT NULL,
   PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Any Suggestions and explanation why Column 'id' cannot be null pop up and how can I resolve. I appreciate it. Thanks!

3
Contributors
5
Replies
9 Hours
Discussion Span
3 Months Ago
Last Updated
7
Views
Question
Answered
LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

I think you forgot to set the id column to auto_increment.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Did you maybe forget to declare the ID colums Autoincrement? That way you can leave the id values out and db will autogenerate them.

Edit: sory pritaeas, posted just seconds after you did :-)

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

@pritaeas
@broj1

Thanks for the reply and explanation! I will make some adjustments

LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

@pritaeas
@broj1

It works! Thanks!

I have question. I mean I always been using that format for the past year without having an issue until now. So it would be a good practice to include auto_increment to be part of my .sql file?

CREATE TABLE `mypost` (
`id` int(15) NOT NULL, auto_increment,
`title` varchar(150) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `username` (
`id` int(15) NOT NULL, auto_increment,
`username` varchar(25) NOT NULL,
`password` varchar(250) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
Question Answered as of 3 Months Ago by pritaeas and broj1

In most cases you need to declare a primary key (unique field) in a table so you can address particular records. If your data already has a field that is unique (such as social security number in the USA or enrollment number on universities) then you can use that, but you have to be sure it is unique (no two records can have the same value). If you haven't got such a field in your data then you usually let database create it for you by using autoincrement type of the field.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1300 seconds using 2.73MB