alir22456 Posted July 28, 2022 Share Posted July 28, 2022 (edited) 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? Edited July 28, 2022 by alir22456 Quote Link to comment Share on other sites More sharing options...
gw1500se Posted July 28, 2022 Share Posted July 28, 2022 Given all the information you are not providing, the best that can be said is that there is a bug in your code. 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 28, 2022 Share Posted July 28, 2022 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. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 28, 2022 Share Posted July 28, 2022 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. 1 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 30, 2022 Share Posted July 30, 2022 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 | +----+-------------+----------+ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.