This is my php and html code.
// This script performs an INSERT query to add a record to the events table
$page_title = 'Machinovate | Add Events';
include ('header_after_login.php');
// Check for form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require ('../../mysqli_connect.php'); // Connect to the db.
$dbc = mysqli_connect("localhost", "username", "password", "machinovate");
$errors = array(); // Initialize an error array.
// Check for a event_date:
if (empty($_POST['event_date']))
{
$errors[] = 'You forgot to enter the event date.';
}
else
{
$event_date = mysqli_real_escape_string($dbc, trim($_POST['event_date']));
}
// Check for a event name:
if (empty($_POST['event_name']))
{
$errors[] = 'You forgot to enter the event name.';
}
else
{
$event_name = mysqli_real_escape_string($dbc, trim($_POST['event_name']));
}
// Check for an event_place address:
if (empty($_POST['event_place']))
{
$errors[] = 'You forgot to enter your event place .';
}
else
{
$event_place = mysqli_real_escape_string($dbc, trim($_POST['event_place']));
}
// Check for an image:
if (is_uploaded_file ($_FILES['image']['tmp_name']))
{
// Create a temporary file name:
$temp = '../../uploads/' . md5($_FILES['image']['name']);
// Move the file over:
if (move_uploaded_file($_FILES['image']['tmp_name'], $temp))
{
echo 'The file has been uploaded!
';
// Set the $i variable to the image's name:
$i = $_FILES['image']['name'];
}
else
{ // Couldn't move the file over.
$errors[] = 'The file could not be moved.';
$temp = $_FILES['image']['tmp_name'];
}
}
else
{ // No uploaded file.
$errors[] = 'No file was uploaded.';
$temp = NULL;
}
if (empty($errors))
{ // If everything's OK.
// Register the event in the database...
// Make the query: **made by drei*
$q = "INSERT INTO events (event_date, event_name, event_place) VALUES (?, ?, ?);
INSERT INTO event_pictures (event_id, image_name) VALUES ((SELECT event_id FROM events WHERE event_date = ?), ?);
";
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 'ssss', $event_date, $event_name, $event_place, $event_date, $i);
mysqli_stmt_execute($stmt);
//Check the results...
if (mysqli_stmt_affected_rows($stmt) == 1)
{ // If it ran OK.
// Print a message:
echo 'Thank you!
An event has been added!!
';
// Rename the image:
$id = mysqli_stmt_insert_id($dbc); // Get the print ID.
rename ($temp, "../../uploads/$id");
// Clear $_POST:
$_POST = array();
}
else
{ // If it did not run OK.
// Public message:
echo 'System Error
The event could not be registered due to a system error. We apologize for any inconvenience.
';
// Debugging message:
echo '' . mysqli_error($dbc) . '
Query: ' . $q . '
';
} // End of if ($r) IF.
mysqli_close($stmt);
} //end of $errors IF
// Delete the uploaded file if it still exists:
if ( isset($temp) && file_exists ($temp) && is_file($temp) )
{
unlink ($temp);
}
} //End of Submission IF
// Check for any errors and print them:
if ( !empty($errors) && is_array($errors) )
{
echo 'Error!
The following error(s) occurred:
';
foreach ($errors as $msg)
{
echo " - $msg
\n";
}
echo 'Please reselect the print image and try again.
';
}
?>
But after running it, I always get an error of:
' Warning: mysqli_stmt_bind_param() expects parameter 1 to be
mysqli_stmt, boolean given in
C:\Users\Angela\Documents\3CSC\ICS114_installers\XAMPP\htdocs\Machinovate\web\admin\add_events.php
on line 89
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt,
boolean given in
C:\Users\Angela\Documents\3CSC\ICS114_installers\XAMPP\htdocs\Machinovate\web\admin\add_events.php
on line 90
Warning: mysqli_stmt_affected_rows() expects parameter 1 to be
mysqli_stmt, boolean given in
C:\Users\Angela\Documents\3CSC\ICS114_installers\XAMPP\htdocs\Machinovate\web\admin\add_events.php
on line 93 System Error The event could not be registered due to a
system error. We apologize for any inconvenience.
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'INSERT INTO event_pictures (event_id, image_name) VALUES
((SELECT event_id FROM ' at line 2
Query: INSERT INTO events (event_date, event_name, event_place) VALUES
(?, ?, ?); INSERT INTO event_pictures (event_id, image_name) VALUES
((SELECT event_id FROM events WHERE event_date=?), ?);
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean
given in
C:\Users\Angela\Documents\3CSC\ICS114_installers\XAMPP\htdocs\Machinovate\web\admin\add_events.php
on line 119'
Please help :( I've been stuck with this for hours already and it's driving me crazy.
No comments:
Post a Comment