-1

I have a school managment software built with PHP. When I run it on my local server it works fine but when I uploaded it to a remote server it gives me an error:

Error Number: 1364

Field 'religion' doesn't have a default value

INSERT INTO `teacher` (
    `name`,
    `birthday`,
    `sex`,
    `address`,
    `phone`,
    `email`,
    `password`
) VALUES (
    'teacher1',
    '08/09/2016',
    'male',
    'Dir Upper Pakistan',
    '0944840412',
    'teacher@example.com',
    '7110eda4d09e062aa5e4a390b0a572ac0d2c0220'
);

Filename: controllers/Admin.php

Line Number: 41

Also, it works fine on a free webhosting server but on another web host it sometimes works and sometimes gives the error.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • 4
    Sounds like `religion` is set to `NOT NULL` and as the error says, doesn't have a default. You'll need to either set the default on the column, or add it to the insert. – Jonnix Aug 09 '16 at 09:50

2 Answers2

0

Looks like the root of the issue is database table strucutre difference between local environment and live. Try ONE of the following solutions:

1.Edit your query in php to include religion:

INSERT INTO teacher (name, birthday, sex, address, phone, email, password, religion)
VALUES ('teacher1', '08/09/2016', 'male', 'Dir Upper Pakistan', '0944840412', 'teacher@example.com', '7110eda4d09e062aa5e4a390b0a572ac0d2c0220', 'myreligion')

2.Update your table structure so religion field is set to allow null values

Serg
  • 22,285
  • 5
  • 21
  • 48
Jason
  • 326
  • 1
  • 12
0

In simple terms, all fields in the database table can be set to have a default value. If you run an INSERT query and don't explicitly set one or more fields, the database will try to create the row using the default value for those fields.

However, in your case the religion field in your database does not have a default value defined. So because you didn't tell it any other value to use for religion, it doesn't know what to do.

You say it sometimes works and sometimes doesn't. This could be because you sometimes set the religion, and sometimes leave it empty in your web form, perhaps.

The best solution would be to go to your database and modify the teacher table so that religion has a default value of NULL. Null basically means "nothing", so when you create rows without specifying a religion, it will just put NULL in that column.

You can run a SQL query to do that:

How do I modify a MySQL column to allow NULL?

Or if you are unsure how to do that, chances are you can log in to your web host account and there will be an option to manage the database - probably through phpMyAdmin. Find the database table you want to edit, then go to the Structure tab, then click Change or Edit on the religion field. Set the default to NULL.

Community
  • 1
  • 1
BadHorsie
  • 14,135
  • 30
  • 117
  • 191