I am trying to exit a script if a date is found from database with an if statement.
However when the date is found (which works) and I use either an exit(); or die(); or return or even throw an exception, it doesn't stop the following INSERT statement from running, & I end up with a new entry in the DB which I don't want. I just can't figure it out, every question I've found here says exit(); or die(); should work.
I've tried moving the curly bracket to the bottom of this page but it still runs the INSERT statement.
The main culprit is in-between /*/////////////////88888888888\\\\\\\\\*/ with the INSERT after it.
Here's my code accumulated from this site:
$servername = "localhost:3308"; $username = "root"; $password = ""; $dbname = "datepicker_test_outputs"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT date FROM bookings"; $result = $conn->query($sql); if ($result->num_rows > 0) { $for_JS_date = null; // output data of each row while($row = $result->fetch_assoc()) { $php_array_dates = date('Y-m-d',strtotime($row["date"])); $for_JS_date .= "'".$php_array_dates."'".","; $correct_date_format = substr($for_JS_date, 0, -1); } if($_SERVER["REQUEST_METHOD"]=="POST"){ $start_date = $_POST["from"]; $end_date = $_POST["to"]; $caravan = $_POST["caravan"]; $dateentry = array(); // populate $dateentry array with dates while (strtotime($start_date) <= strtotime($end_date)) { $dateentry[] = date("Y-m-d", strtotime($start_date)); $start_date = date ("Y-m-d", strtotime("+1 day", strtotime($start_date))); } // end while // loop through $dateentry and insert each date into database foreach($dateentry as $entry) { /*/////////////////88888888888\\\\\\\\\\\\\\\\\\\*/ $string = $correct_date_format; $word = $entry; if(strpos($string, $word) !== false){ echo "BINGO! IT WAS FOUND '$word'"; echo "<p style='color:red;'>" . $correct_date_format . "</p>"; /* header("Location: re_direct_page.php"); return; die(); throw new Exception('An Error Ocurred');*/ exit(); }else{ echo "The word '$word' was not found in the given string"; } /*/////////////////88888888888\\\\\\\\\\\\\\\\\\\*/ $my_inserted_dates =("INSERT INTO bookings (date,caravan_id) VALUES('$entry', '$caravan')") or die(mysqli_error()); $result = mysqli_query($conn,$my_inserted_dates); } if($result == false) { echo "<script>alert('BOOKING IS NOT SUCCESS - PLEASE CONTACT ADMINISTRATOR'); </script>"; } else { /* Header location to refresh the page & load booked dates */ header('Location: '.$_SERVER['PHP_SELF']); } die(); // end foreach } }
https://stackoverflow.com/questions/67352356/php-exit-die-return-does-not-stop-insert-script-from-executing May 02, 2021 at 09:53AM
没有评论:
发表评论