1.11M Members

Column 'id' cannot be null

LastMitch
Deleted Member
 
0
 

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!

 
1
 

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

 
2
 

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 :-)

LastMitch
Deleted Member
 
0
 

@pritaeas
@broj1

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

LastMitch
Deleted Member
 
0
 

@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;
Question Answered as of 1 Year Ago by pritaeas and broj1
 
1
 

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.

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: