Question
how to slove this error HTTP ERROR 500
<?php
// Upload configs.
define('UPLOAD_DIR', 'uploads',);
define('UPLOAD_MAX_FILE_SIZE', 10485760); // 10MB.
//@changed_2018-02-17_14.28
define('UPLOAD_ALLOWED_MIME_TYPES', 'image/jpeg,image/png,image/gif');
// Db configs.
define('HOST', '127.0.0.1');
define('DATABASE', 'krishnasweets_shop');
define('USERNAME', 'krishnasweets_shop');
define('PASSWORD', 'kxv7aKVUg0');
define('CHARSET', 'utf8');
/*
* Enable internal report functions. This enables the exception handling,
* e.g. mysqli will not throw PHP warnings anymore, but mysqli exceptions
* (mysqli_sql_exception).
*
* MYSQLI_REPORT_ERROR: Report errors from mysqli function calls.
* MYSQLI_REPORT_STRICT: Throw a mysqli_sql_exception for errors instead of warnings.
*
* @link http://php.net/manual/en/class.mysqli-driver.php
* @link http://php.net/manual/en/mysqli-driver.report-mode.php
* @link http://php.net/manual/en/mysqli.constants.php
*/
$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
/*
* Create a new db connection.
*
* @see http://php.net/manual/en/mysqli.construct.php
*/
$connection = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
$productSaved = FALSE;
if (isset($_POST['submit'])) {
/*
* Read posted values.
*/
$productName = isset($_POST['title']) ? $_POST['title'] : '';
$productSpecification = isset($_POST['specification']) ? $_POST['specification'] : '';
$productCategory = isset($_POST['menu_id']) ? $_POST['menu_id'] : '';
$productPrice = isset($_POST['price']) ? $_POST['price'] :'';
$productDescription = isset($_POST['description']) ? $_POST['description'] : '';
/*
* Validate posted values.
*/
if (empty($productName)) {
$errors[] = 'Please provide a product name.';
}
if ($productPrice == 0) {
$errors[] = 'Please provide the quantity.';
}
if (empty($productDescription)) {
$errors[] = 'Please provide a description.';
}
if (empty($productSpecification)) {
$errors[] = 'Please provide a Specification.';
}
if (empty($productCategory)) {
$errors[] = 'Please provide a Category.';
}
/*
* Create "uploads" directory if it doesn't exist.
*/
if (!is_dir(UPLOAD_DIR)) {
mkdir(UPLOAD_DIR, 0777, true);
}
/*
* List of file names to be filled in by the upload script
* below and to be saved in the db table "products_images" afterwards.
*/
$filenamesToSave = [];
$allowedMimeTypes = explode(',', UPLOAD_ALLOWED_MIME_TYPES);
/*
* Upload files.
*/
if (!empty($_FILES)) {
if (isset($_FILES['file']['error'])) {
foreach ($_FILES['file']['error'] as $uploadedFileKey => $uploadedFileError) {
if ($uploadedFileError === UPLOAD_ERR_NO_FILE) {
$errors[] = 'You did not provide any files.';
} elseif ($uploadedFileError === UPLOAD_ERR_OK) {
$uploadedFileName = basename($_FILES['file']['name'][$uploadedFileKey]);
if ($_FILES['file']['size'][$uploadedFileKey] <= UPLOAD_MAX_FILE_SIZE) {
$uploadedFileType = $_FILES['file']['type'][$uploadedFileKey];
$uploadedFileTempName = $_FILES['file']['tmp_name'][$uploadedFileKey];
$uploadedFilePath = rtrim(UPLOAD_DIR, '/') . '/' . $uploadedFileName;
if (in_array($uploadedFileType, $allowedMimeTypes)) {
if (!move_uploaded_file($uploadedFileTempName, $uploadedFilePath)) {
$errors[] = 'The file "' . $uploadedFileName . '" could not be uploaded.';
} else {
$filenamesToSave[] = $uploadedFilePath;
}
} else {
$errors[] = 'The extension of the file "' . $uploadedFileName . '" is not valid. Allowed extensions: JPG, JPEG, PNG, or GIF.';
}
} else {
$errors[] = 'The size of the file "' . $uploadedFileName . '" must be of max. ' . (UPLOAD_MAX_FILE_SIZE / 1024) . ' KB';
}
}
}
}
}
/*
* Save product and images.
*/
if (!isset($errors)) {
/*
* The SQL statement to be prepared. Notice the so-called markers,
* e.g. the "?" signs. They will be replaced later with the
* corresponding values when using mysqli_stmt::bind_param.
*
* @link http://php.net/manual/en/mysqli.prepare.php
*/
$sql = 'INSERT INTO products (
title,
price,
specification,
menu_id,
description
) VALUES (
?, ?, ?, ?, ?
)';
/*
* Prepare the SQL statement for execution - ONLY ONCE.
*
* @link http://php.net/manual/en/mysqli.prepare.php
*/
$statement = $connection->prepare($sql);
/*
* Bind variables for the parameter markers (?) in the
* SQL statement that was passed to prepare(). The first
* argument of bind_param() is a string that contains one
* or more characters which specify the types for the
* corresponding bind variables.
*
* @link http://php.net/manual/en/mysqli-stmt.bind-param.php
*/
$statement->bind_param('sssss', $productName, $productPrice,
$productSpecification, $productCategory,$productDescription);
/*
* Execute the prepared SQL statement.
* When executed any parameter markers which exist will
* automatically be replaced with the appropriate data.
*
* @link http://php.net/manual/en/mysqli-stmt.execute.php
*/
$statement->execute();
// Read the id of the inserted product.
$lastInsertId = $connection->insert_id;
/*
* Close the prepared statement. It also deallocates the statement handle.
* If the statement has pending or unread results, it cancels them
* so that the next query can be executed.
*
* @link http://php.net/manual/en/mysqli-stmt.close.php
*/
$statement->close();
if(!$statement)
{
echo mysqli_error($statement);
}
/*
* Save a record for each uploaded file.
*/
foreach ($filenamesToSave as $filename) {
$sql = 'INSERT INTO products_images (
product_id,
filename
) VALUES (
?, ?
)';
$statement = $connection->prepare($sql);
$statement->bind_param('is', $lastInsertId, $filename);
$statement->execute();
$statement->close();
}
/*
* Close the previously opened database connection.
*
* @link http://php.net/manual/en/mysqli.close.php
*/
$connection->close();
if($connection)
{
header('Location: addproduct.php?success=true');
}
$productSaved = TRUE;
/*
* Reset the posted values, so that the default ones are now showed in the form.
* See the "value" attribute of each html input.
*/
$productName = $productPrice = $productDescription = NULL;
}
}
?>
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×