Jump to content

A record is not saved in the database if the fields (not mandatory) are empty


alir22456

Recommended Posts

WordPress Plugin: The School Management – Education & Learning Management

 

I am trying to save a students record in the database. There are two fields (Class, Section) that were mandatory before, and because they were mandatory, so I had to select a value for both fields. The student’s record was saved in the database. But as per the requirement, I don’t need the fields to be mandatory. That’s why I made them non-mandatory fields.

But now the problem is if I leave the class and section fields empty the student record is not saved in the database.  After submitting the message appears as:

 

Admission added successfully 

But if I check the database the student is not saved.

 

Can someone explain what the problem might be?

1.png

2.png

Edited by alir22456
Link to comment
Share on other sites

You way that those 2 fields were mandatory before.  And now they are not.  Well just who decided to do that?  And if they did do that, did they make the necessary code changes (in the validation steps most likely) to allow for that change?  That's where your problem is.

Link to comment
Share on other sites

1 hour ago, alir22456 said:

That’s why I made them non-mandatory fields.

Quick question: how did you make them optional fields? Is there something in the plugin interface that lets you choose whether a field is optional or required? If so, you may need to contact the plugin developer to see what's happening.

However, if you're using custom code to handle what happens with required/optional fields, you'll need to do some debugging as the others have suggested.

  • Great Answer 1
Link to comment
Share on other sites

My theory is that class and section are defined as foreign keys in the student table. Even though they are no longer mandatory is not sufficient just to to leave them as blank values.

Consider this scenario with class table and student table. Class_id in the student table is defined as a foreign key, so id values in the that table must match a perent records id in the class table.

    CREATE TABLE `class` (                              CREATE TABLE `student` (
      `id` char(2) NOT NULL,                              `id` int(11) NOT NULL,
      `classname` varchar(20) DEFAULT NULL,               `studentname` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`id`)                                  `class_id` char(2) DEFAULT NULL,
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;                 PRIMARY KEY (`id`),
                                                          KEY `idx_student_class_idx` (`class_id`),
                                                          CONSTRAINT `idx_student_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`)
                                                                      ON DELETE NO ACTION 
                                                                      ON UPDATE NO ACTION
                                                        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    +----+-----------+                                  +----+-------------+----------+
    | id | classname |                                  | id | studentname | class_id |
    +----+-----------+                                  +----+-------------+----------+
    | A1 | Class A   |                                  |  1 | John        | A1       |
    | A2 | Class B   |                                  |  2 | Jane        | A2       |
    | A3 | Class C   |                                  |  3 | Curly       | A3       |
    +----+-----------+                                  +----+-------------+----------+

 

If we now attempt to insert a new student with a blank class_id

insert into student (id, studentname, class_id) 
values (4, 'Larry', '' );

result:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`student`, CONSTRAINT `idx_student_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

 

The way around this is explicitly to write NULL into the class_id of the student record

insert into student (id, studentname, class_id) 
values (5, 'Mo'   , null);

Query OK, 1 row affected (0.06 sec)

+----+-------------+----------+
| id | studentname | class_id |
+----+-------------+----------+
|  1 | John        | A1       |
|  2 | Jane        | A2       |
|  3 | Curly       | A3       |
|  5 | Mo          | NULL     |
+----+-------------+----------+

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.