I made a PHP site that uses MySQLi with basic functionality to submit an HTML5 form into my database. The problem is that it will only insert into the database if ALL fields have been completed. How can I make it so that I can leave some fields blank, but have it work properly for the rest?
//connection statements above
$first=$_POST['first'];
$last=$_POST['last'];
$signed=$_POST['signed'];
$cardnumb=$_POST['cardnumb'];
$perm=$_POST['permcard'];
$stmt = $mysqli->prepare("INSERT INTO information (first_name, last_name, signed, number, permanent) VALUES (?,?,?,?,?)");
$stmt->bind_param("sssss", $first, $last, $signed, $cardnumb, $perm);
$stmt->execute();
$stmt->close();
Just to be clear, this code works perfectly fine if ALL fields have been filled out. It DOES NOT work if a field is left empty.
Is there a way to make this work with empty fields?
If you don’t fill out the field does the
$_POST['varname'];returnnullor an empty string?If its an empty string this should still work fine.
If its
nullthen it should work as long as the fields are allowed to benullin the database.Check your table scheme and make sure the fields are allowed to be
null.