I am facing very strange issue which is that record is not entering in database when I do not entered data in fields which are optional. Age is option field in my form and below image of database table also showing age can be null. (Remarks is also optional, and if i do no entered value in remarks field, then record successfully entered in database table, where as in age1, when I left it blank, the record did not store in database ) here is my form code
Are you entering a duplicate mobile phone number? I noticed you don’t trap the error that will be returned if you do.
How far through your code does it get when you debug it? If you echo $sql and then paste the query directly into phpmyadmin, does it work then, or does it give you a useful error message?
i mentioned my column unique in database, value is nto entered for duplicate record, but it also not generting error message, i want to generate error message
By definition all set $_POST form fields are strings, regardless of what value they contain. Only unchecked checkbox or radio fields won’t be set and would be null values. An empty text type form field will be an empty string and your NULL = Yes setting will have no effect.
The only ways you can cause a null value to get used for a column are -
use the sql NULL keyword or the sql DEFAULT keyword in the sql statement for the value, which your current use of putting the variables directly into the sql query statement surrounded by single-quotes cannot accomplish. You would need to dynamically build the sql query statement and put the literal NULL or DEFAULT keyword into the sql query syntax.
dynamically build the sql query statement leaving out the column and its value, which will cause the defined default value to be used.
use a (true) prepared query (this won’t work with a pdo emulated prepared query), where a null value will be passed through to the database server.
Therefore, something outside the information you have posted in this thread is occurring.
You ALWAYS need error handling for statements that can fail. For database statements that can fail - connection, query, exec, prepare, and execute, the simplest way of adding error handling, without adding code at each statement, is to use exceptions for errors (this is the default setting now in php8+) and in most cases simply let php catch and handle the database exceptions, where php will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.)
What php version are you using? If it is 8+, unless you turn off the use of exceptions for errors (not recommended), any database statement error handling failure conditional logic you have now won’t ever get executed, since execution transfers to the nearest correct type of exception handling, or to php if there is no exception handling in your code.
Do you have php’s error_reporting set to E_ALL and dispaly_errors set to ON, preferably in the php.ini on your development system, so that php will help you by reporting and displaying all the errors it detects? Stop and start your web server to get any changes made to the php.ini to take effect and use a phpinfo() statement in a .php script to confirm that the settings actually took effect.
Lastly, ALL the post method form processing code needs to be inside the conditional statement testing if a post method form was submitted.
The general way of handling this is to test for a duplicate index error number (1062) in the try/catch exception handler for the INSERT query (which you will need to add) and setup a message for the user letting them know that the value already exists. For all other error numbers, just rethrow the exception and let php handle it.
Slightly off topic, but in your earlier threads with database queries, you were using prepared queries. Why are you now putting php variables/values directly into the sql query statement, where any sql special character in a value can break the sql query syntax, which is how sql injection is accomplished?
If this is because of how difficult it is to use the mysqli extension with prepared queries, this would be a good time to switch to the much simpler and more modern PDO extension. After you make the PDO, connection (if you need to see typical PDO connection code, someone can post it), the INSERT query you have shown in this thread would be -
A. Yes it will work.
B. You could always try the things that have been suggested and observe the result, so that you would learn for yourself if the stated method works.