So I have a PHP form to add a row to a database. If any of the unrequired fields are empty, the default value for that field should be used.
I tried to implement this with MySQLi like this:
$required = $_POST["required"];
$unrequired = isset($_POST["unrequired"])?$_POST["unrequired"]:"DEFAULT(`Unrequired`)";
$sql = $mysqli->prepare("INSERT INTO `table` (`Required`,`Unrequired`) VALUES (?,?)");
$sql->bind_param("is",$required,$unrequired);
$sql->execute();
But when I try to get the value of the unrequired field using SELECT unrequired FROM table WHERE required = 33, I get DEFAULT(`Unrequired`) instead of the default value of the column for varchar columns, and 0 for int and double columns.
Is this problem caused by PHP, or MySQL?
NOTE: Some of the unrequired fields are nullable and some are not. Those which aren't nullable have a set default value. int and double fields' set default value is 1, the rest are nullable.
Unrequired)? It should be returned DEFAULT(Unrequired) and it will be inserted in your table. You can set default value instead of DEFAULT(Unrequired).