You may in fact have php’s error_reporting/display_errors set (based on the php syntax error being reported in your previous thread) to get php to help you, but getting php to report and display all the errors it detects is just one small part of writing and debugging code.
To check this, create a php script file with <?php phpinfo(); ?> in it, browse to the url of the file, and check what the error_reporting and display_errors settings are. error_reporting should be the value 32767 (all bits set) and display_errors should be the value ON.
Your code needs to ALWAYS have error handling and validation logic in it so that your code will tell the visitor (and you when developing and debugging it) when and why it is failing. Your code should check for all possible errors, then only use the input data it receives if there are no errors. Use a php array variable, $errors, to hold the error messages. At the point of using the input data, if the array is empty, there are no errors. To display the errors, just output the contents of the array, using any styling you want.
Your form processing code needs to detect that a post method form was submitted at all. Testing if a $_POST … variable is set is not sufficient when uploading files (or when the form gets submitted by a method other than clicking on the submit button), because if the total posted data exceeds the post_max_size setting, both the $_POST and $_FILES arrays will be empty (you can learn this type of information by making use of the php.net documentation.)
You can detect if a post method form has been submitted by using -
[php]if($_SERVER[‘REQUEST_METHOD’] == ‘POST’)
{
// your post method form processing code goes here
}[/php]
If a post method form has been submitted (and uploads are enabled on your server and your form is valid), then an empty $_FILES array would indicate that the post_max_size has been exceeded. At a minimum, add logic to test for this condition and add an error message to the $errors array. You can enhance this by building the error message with addition information, such as what the actual size of the submitted data is and what the post_max_size setting it.
If the $_FILES array is not empty, that would mean that the post_max_size has not been exceeded and you can reference the data in the $_FILES array. You would need test if the upload worked, by testing the [‘error’] element of the $_FILES[‘file’] array. Again, you can find information in the php.net documentation about what errors there are. For the errors that the visitor can affect (file size or picking a file), you would add an error message to the $errors array.
If the file was successfully uploaded, you would perform any other validation of the file data, such as the file size, image size, image type, file extension, file name already exists, …
If there are no errors at this point, because the $_FILE data will be lost if you re-display the form to fix any problems with the $_POST field data, you would move the uploaded file from the tmp location to the final location, further testing for any error from the move_uploaded_file() statement.
You would then perform validation of the $_POST data, adding any error messages to the $errors array.
If at this point, there are no errors, you would INSERT the data into the database table. You need to use a prepared query, with place-holders in the sql query statement for the data values, then supply the actual data when you execute the query. Doing this actually simplifies the sql query syntax, making it easier to write a working query.
Unfortunately, the php mysqli extension (what you are using) is poorly designed, especially when using prepared queries, and you should switch to use the php PDO extension. The php PDO extension is simpler and more straight-forward then the mysqli extension, with the added benefit that once you learn the php PDO statements, you can use the same php logic with different database types.
If you use exceptions for the database error handling, you won’t have to add php logic to test the result of every statement. To enable exceptions for the php mysqli extension, add the following line of code before the point where you are making the database connection -
[php]mysqli_report(MYSQLI_REPORT_ALL);[/php]
This line, combined with the suggested php error_reporting/display_errors settings will get php to catch any database errors and display the actual error information for you.
You should also temporarily comment out any header() redirect so that your code will stay on the same page, and the header() redirect you do have needs an exit/die statement to stop program execution.
If you do ALL these things, your code will either work or it and php will give you an indication of when and why it isn’t working.