View Single Post
Old 10-08-2021, 10:45 PM  
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,164
Why isnt my close statement working?

Quote:
<?php

// Include config file
require_once "config.php";

// Define variables and initialize with empty values
$title = $ingredients = $method = $category = "";
$title_err = $ingredients_err = $method_err = $category_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate title
$input_title = trim($_POST["title"]);
if(empty($input_title)){
$title_err = "Please enter a title.";
} elseif(!filter_var($input_title, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$title_err = "Please enter a valid title.";
} else{
$title = $input_title;
}

// Validate ingredients
$input_ingredients = trim($_POST["ingredients"]);
if(empty($input_ingredients)){
$ingredients_err = "Please enter ingredients.";
} else{
$ingredients = $input_ingredients;
}

// Validate method
$input_method = trim($_POST["method"]);
if(empty($input_method)){
$method_err = "Please enter the method.";
} elseif(!filter_var($input_title, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$method_err = "Please enter a method.";
} else{
$smethod = $input_method;
}

// Validate category
$input_category = trim($_POST["category"]);
if(empty($input_category)){
$category_err = "Please enter the Category.";
} elseif(!filter_var($input_category, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$category_err = "Please enter a category.";
} else{
$category = $input_category;
}


// Check input errors before inserting in database
if(empty($title_err) && empty($ingredients_err) && empty($method_err) && empty($category_err)){
// Prepare an insert statement
$sql = "INSERT INTO Recipes (title, ingredients, method, category) VALUES (?, ?, ?, ?)";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $param_title, $param_ingredients, $param_method, $param_category);

// Set parameters
$param_title = $title;
$param_ingredients = $ingredients;
$param_method = $method;
$param_category = $category;

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records created successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}

// Close statement
mysqli_stmt_close($stmt);
}


// Close connection
mysqli_close($link);
}
?>
I don't understand what the issue with it is? I've moved it a couple of times and its still kicking out an error, I'm 99.99% certain its valid(?)

This is on my Create of the CRUD system I'm putting together

Any pointers please?
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote