Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 10-08-2021, 10:45 PM   #1
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,143
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
Old 10-09-2021, 12:36 AM   #2
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
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.
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-09-2021, 02:26 AM   #3
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,143
Quote:
Originally Posted by k0nr4d View Post
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
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-09-2021, 05:29 AM   #4
iwantfreedom
Confirmed User
 
Industry Role:
Join Date: Jul 2019
Posts: 95
Why are you still using mysqli in 2021?
iwantfreedom is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-09-2021, 08:13 AM   #5
Klen
 
Klen's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Little Vienna
Posts: 32,235
Quote:
Originally Posted by iwantfreedom View Post
Why are you still using mysqli in 2021?
You have problem with mysqli ?
Klen is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-09-2021, 11:30 AM   #6
iwantfreedom
Confirmed User
 
Industry Role:
Join Date: Jul 2019
Posts: 95
Quote:
Originally Posted by Klen View Post
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
iwantfreedom is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-09-2021, 02:41 PM   #7
Klen
 
Klen's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Little Vienna
Posts: 32,235
Quote:
Originally Posted by iwantfreedom View Post
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 ?
Klen is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-09-2021, 03:45 PM   #8
Colmike9
(>^_^)b
 
Colmike9's Avatar
 
Industry Role:
Join Date: Dec 2011
Posts: 7,223
Quote:
Originally Posted by Klen View Post
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.
__________________
Join the BEST cam affiliate program on the internet!
I've referred over $1.7mil in spending this past year, you should join in.
I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..
Colmike9 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-10-2021, 01:49 AM   #9
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
Quote:
Originally Posted by Publisher Bucks View Post
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
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks

Tags
99.99%, error, valid, create, crap, pointers, crud, system, putting, understand, close, statement, issue, times, couple, moved, kicking



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.