GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech Why isnt my close statement working? (https://gfy.com/showthread.php?t=1348988)

Publisher Bucks 10-08-2021 10:45 PM

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 :Oh crap

Any pointers please?

k0nr4d 10-09-2021 12:36 AM

You are redirecting to index.php and exit()'ing in the event of success, so you never get to that code nor the mysqli_close.

It's also better to not using a closing ?> *at the end of the file*. If you press enter or a space after ?> for instance in your config.php file which you are including at the top, your header() line might fail because a space or linebreak has already been sent to the output buffer.

Publisher Bucks 10-09-2021 02:26 AM

Quote:

Originally Posted by k0nr4d (Post 22922748)
You are redirecting to index.php and exit()'ing in the event of success, so you never get to that code nor the mysqli_close.

It's also better to not using a closing ?> *at the end of the file*. If you press enter or a space after ?> for instance in your config.php file which you are including at the top, your header() line might fail because a space or linebreak has already been sent to the output buffer.

Ah, okay, that makes sense.

So should I just get rid of those then or do something with the redirection?

This is behind a .htaccess protected directory so I'm not all that concerned about closing out the connection at this moment in time if I don't need to in order to get this working lol

iwantfreedom 10-09-2021 05:29 AM

Why are you still using mysqli in 2021?

Klen 10-09-2021 08:13 AM

Quote:

Originally Posted by iwantfreedom (Post 22922765)
Why are you still using mysqli in 2021?

You have problem with mysqli ?

iwantfreedom 10-09-2021 11:30 AM

Quote:

Originally Posted by Klen (Post 22922795)
You have problem with mysqli ?


Yes, its really old, the standard thing to use has been PDO now for almost a decade.

But TBH, if your just doing crud, it might be better to just use a headless CMS like strapi.io

Klen 10-09-2021 02:41 PM

Quote:

Originally Posted by iwantfreedom (Post 22922861)
Yes, its really old, the standard thing to use has been PDO now for almost a decade.

But TBH, if your just doing crud, it might be better to just use a headless CMS like strapi.io

Why complicate if it's working? Plus isn't mysqli faster ?

Colmike9 10-09-2021 03:45 PM

Quote:

Originally Posted by Klen (Post 22922914)
Why complicate if it's working? Plus isn't mysqli faster ?

A little bit, like 2-5% faster.

One thing about PDO vs mysqli is that PDO has like a dozen db drivers, mysqli just does mysql. Switching, if you ever have to, will be a lot easier and quicker on PDO.

k0nr4d 10-10-2021 01:49 AM

Quote:

Originally Posted by Publisher Bucks (Post 22922757)
Ah, okay, that makes sense.

So should I just get rid of those then or do something with the redirection?

This is behind a .htaccess protected directory so I'm not all that concerned about closing out the connection at this moment in time if I don't need to in order to get this working lol

Put it before the header() line


All times are GMT -7. The time now is 11:32 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123