0

When I click on an 'Event' in my list of upcoming events page, it opens that particular event's EditEvent page. The page retrieves the information that exists from the database to fill the input fields of the form without any issues. However, clicking the submit button to save the changes to the database outputs a user-friendly error stating "We ran into a problem updating the event".

This is useful for debugging, but unfortunately I cannot find the issue and so I need help. Thanks for the help in advance!

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8" />
    <title>Edit Event</title>
</head>
<body>

    <?php
        // Declare variables for database and server connection
        $serv_name = "localhost";
        $db_user = "root";
        $db_pass = "";
        $db_name = "cmeteventmanagement";

        // Declare variable to use MySQLI database connection
        $connect = new mysqli($serv_name, $db_user, $db_pass, $db_name);

        // Define variables and set to empty values
        $title = "";
        $description = "";
        $location = "";
        $date = "";
        $startTime = "";
        $duration = "";

        // Declare variables to hold potential error messages for each field.
        $titleError = "";
        $descriptionError = "";
        $locationError = "";
        $dateError = "";
        $startTimeError = "";
        $durationError = "";
        $dbConnectionErr = false;
        $dbRetrievalErr = false;
        $dbUpdateErr = false;
        $dbGeneralErr = false;

        // When the page loads AND if method is GET
        if($_SERVER["REQUEST_METHOD"] == "GET")
        {
            // If there is an error connecting, show error message
            if ($connect->connect_error)
            {
                $dbConnectionErr = true;
            }
            else
            {
                // Retrieve data for current event
                $retrieve = "SELECT * FROM tbl_events WHERE event_id=?";
                $stmt = $connect->prepare($retrieve);
                $stmt->bind_param('i', $_GET['event_id']);
                $stmt->execute();
                $result = $stmt->get_result();

                if ($result->num_rows <= 0)
                {
                    // If there is an error matching the event ID with the database, show error message (in HTML)
                    $dbRetrievalErr = true;
                }
                else if ($result->num_rows === 1)
                {
                    // If inputted data has a match (can only be one because event ID is unique)
                    // Get row data from table to apply details into input fields
                    $row = $result->fetch_assoc();
                    $title = $row['event_title'];
                    $description = $row['event_description'];
                    $location = $row['event_location'];
                    $date = $row['event_date'];
                    $startTime = $row['event_start_time'];
                    $duration = $row['event_duration_mins'];
                }
                else
                {
                    // In all other cases, something went wrong, show error (in HTML)
                    $dbGeneralErr = true;
                }
            }
        }


    ?>

    <?php
        // If the form has been submitted, AND the method is POST
        if ($_SERVER["REQUEST_METHOD"] == "POST")
        {
            // Post user inputs from form fields
            $title = $_POST['title'];
            $description = $_POST['description'];
            $location = $_POST['location'];
            $date = $_POST['date'];
            $startTime = $_POST['startTime'];
            $duration = $_POST['duration'];

            // Declare variables to display errors
            $foundErrors = false;

            // TITLE
            if (empty($_POST['title']))
            {
                $titleError = "Title is required";
                $foundErrors = true;
            }

            // DESCRIPTION
            if (empty($_POST['description']))
            {
                $descriptionError = "Description is required";
                $foundErrors = true;
            }

            // LOCATION
            if (empty($_POST['location']))
            {
                $locationError = "Location is required";
                $foundErrors = true;
            }

            // DATE
            if (empty($_POST['date']))
            {
                $dateError = "Date is required";
                $foundErrors = true;
            }

            // START TIME
            if (empty($_POST['startTime']))
            {
                $startTimeError = "Start Time is required";
                $foundErrors = true;
            }

            // DURATION
            if (empty($_POST['duration']))
            {
                $durationError = "Duration is required";
                $foundErrors = true;
            }

            // If no errors were found
            if ($foundErrors === false)
            {
                // Check that a connection still exists to our database
                if ($connect->connect_error)
                {
                    $dbConnectionErr = true;
                }
                else
                {
                    // Prepare SQL statements to update data
                    $update = "UPDATE tbl_events SET event_title=?, event_description=?, event_location=?, event_date=?, event_start_time=?, event_duration_mins=? WHERE event_id=?";
                    $stmt_edit = $connect->prepare($update);
                    $date = strtr($_REQUEST['date'], '/', '-');     // converts HTML date to SQL date
                    $stmt_edit->bind_param('sssssii', $title, $description, $location, $date, $startTime, $duration, $_GET['event_id']);    // Gets event_id passed in to URL
                    $result_edit = $stmt_edit->execute();

                    // If any rows in our events table were affected, confirm success/redirect
                    if ($result_edit && $stmt_edit->affected_rows > 0)
                    {
                        header("Location: events.php");
                    }
                    else
                    {
                        $dbUpdateErr = true;    // !!! REACHES THIS ERROR !!!
                    }
                }
            }
        }
    ?>

    <!-- Header Links -->
    <a href="logout.php">Log Out</a>
    <br><br>
    <a href="events.php">Back to Events</a>

    <!-- Page Title -->
    <h1>Editting Event</h1>

        <!-- Main Content -->
        <form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off" id="mainForm" enctype="multipart/form-data">
        Title:
            <br>
            <input type="text" name="title" maxlength="70" value="<?php echo $title ?>">
            <span> * <?php echo $titleError; ?></span>
            <br><br>
        Description (max 1500 characters):
            <br>
            <textarea name="description" form="mainForm" maxlength="1500" style="width:450px;height:150px;"><?php echo $description ?></textarea>
            <span> * <?php echo $descriptionError; ?></span>
            <br><br>
        Location:
            <br>
            <input type="text" name="location" maxlength="150" value="<?php echo $location ?>">
            <span> * <?php echo $locationError; ?></span>
            <br><br>
        Date:
            <br>
            <input type="date" name="date" value="<?php echo $date ?>">
            <span> * <?php echo $dateError; ?></span>
            <br><br>
        Start Time:
            <br>
            <input type="time" name="startTime" value="<?php echo $startTime ?>">
            <span> * <?php echo $startTimeError; ?></span>
            <br><br>
        Duration:
            <br>
            <input type="text" name="duration" maxlength="4" value="<?php echo $duration ?>">
            <span>minutes * <?php echo $durationError; ?></span>
            <br><br>
        <input type="submit" value="Save Changes">
    </form>

    <!-- Section to display any error messages IF they occur -->
    <br>
    <?php if ($dbRetrievalErr === true): ?><p>We ran into a problem retrieving the current event data</p><?php endif; ?>
    <?php if ($dbUpdateErr === true): ?><p>We ran into a problem updating the event</p><?php endif; ?>  <!-- !!! REACHES THIS ERROR !!! -->
    <?php if ($dbGeneralErr === true): ?><p>Something went wrong while searching our records</p><?php endif; ?>
    <?php if ($dbConnectionErr === true): ?><p>Error connecting to the database</p><?php endif; ?>

</body>

</html>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Use `$stmt_edit->error` to see the actual error. – Qirel Apr 14 '20 at 11:43
  • Doing so actually doesn't output an error - tested with string concatenation. Unless I'm doing it wrong: Within the final ELSE statement I put: $error = "error: " . $stmt_edit->error; At the end of my HTML below all the other errors I put:

    Output: "Error: "
    – Master_Templar Apr 14 '20 at 12:08
  • Followed the suggested question's guidance, code works fine in that regard, no errors are outputted in the 3 different stages. *EDIT* but still having the same issue as per my original question. – Master_Templar Apr 14 '20 at 12:24
  • Then it's possible that `$_GET['event_id']` didn't match any rows, and you can check the value of `$stmt_edit->affected_rows`. – Qirel Apr 14 '20 at 13:10
  • Confirmed that it returns 0 on affected rows in an output. However I fail to see why, as I use $_GET['event_id'] in order to populate the form fields in the first place in the lines:`$retrieve = "SELECT * FROM tbl_events WHERE event_id=?"; $stmt = $connect->prepare($retrieve); $stmt->bind_param('i', $_GET['event_id']);` – Master_Templar Apr 14 '20 at 13:31

0 Answers0