Member Avatar for LastMitch

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!

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

commented: Thanks for the Solution +9

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

commented: Thanks for the explanation! +9
Member Avatar for LastMitch

@pritaeas
@broj1

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

Member Avatar for LastMitch

@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;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.