This is my code
<form action="event_feed.php" method="POST">
<label for="title">Event Title:</label>
<input type="text" name="title" id="title" required>
<label for="description">Description:</label>
<textarea name="description" id="description"></textarea>
<label for="date">Date:</label>
<input type="date" name="date" id="date" required>
<label for="time">Time:</label>
<input type="time" name="time" id="time" required>
<label for="location">Location:</label>
<input type="text" name="location" id="location" required>
<input type="submit" name="create_event" value="Create Event">
</form>
<?php
// this is for creating event
// Process form data when form is submitted
if (isset($_POST['create_event'])) {
// Get form data
$title = $_POST['title'];
$description = $_POST['description'];
$date = $_POST['date'];
$time = $_POST['time'];
$location = $_POST['location'];
// Insert data into database
$queryCreate = "INSERT INTO events (Event_title, Description, Date, Time, Location) VALUES ('$title', '$description', '$date', '$time', '$location')";
$sqlCreate = mysqli_query($conn, $queryCreate);
} else {
echo '<script>alert("There was an error, please try again")</script>';
}
?>
Hello There I was Creating our capstone my question is why
else {
echo '<script>alert("There was an error, please try again")</script>';
}
is always executing even if I didn’t do any activity?
I expect that my code was correct but there was an error that i need to resolve, When i clicked the ‘event’ page the else statement is shown
2
Answers
Looking at your code, the else statement will be triggered if the condition
isset($_POST['create_event'])
isfalse
. This means that the else block will be executed when the ‘create_event’ variable is not set in the$_POST
array.You could try to
dd(isset($_POST['create_event']));
(assuming you are using Laravel, it was before in the post tags) or Log it, to see if it evaluates true or false. Also check if there could be an iteration or it enters the method more than 1 time.You might need to use in_array instead of isset. in_array will return true if the array element exists but is set to NULL. isset will return false even if the element exists but contains NULL as a value.