I know this is a really discussed thread, I've asked one question not so long ago AND it's a topic discussed countless times.
But, I'm still trying to secure a web app using prepared statements.
Truth is, never really got it to work with the answers given, looking at the duplicate question, looking at other questions and doing research.
It can't be that complex...
So, I have a lcl_events.php
file, that starts with:
<?php include 'config/config.php'; ?>
<?php include'libraries/database.php'; ?>
The database.php
file, looks like this:
<?php
// Create connection
$mysqli= new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_errno) {
die("Connection failed: " . mysqli_connect_error());
}
?>
The page loads fine, so far so good (no problems here).
Then, the file itself has also:
<?php
$sql = "select *
from companies where Company_Name LIKE (?) OR Company_Subcategory LIKE (?) OR Keywords LIKE (?) OR Description LIKE (?) AND Company_Category = (?) AND Featured = `Y` order by Date_Created DESC";
/* Prepared statement, stage 1: prepare */
$stmt = $mysqli->prepare($sql);
if(!$stmt) {
die("Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error);
}
/* Prepared statement, stage 2: bind and execute */
$target = $_GET['target'];
$company = $_GET['company'];
$category = $_GET['category'];
$target = '%'.$target.'%'; //this means the data coming from this GET method can have words before and/or after
$bind_result = $stmt->bind_param("sssss", $target, $target, $target, $target, $category);
$execute_result = $stmt->execute();
if(!$execute_result) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {}
/* explicit close recommended */
$stmt->close();
$mysqli->close();
The same process is repeated 6 times throughout the page.
(tried also to write the query without round brackets around the question mark.
The result is the following:
No errors appear in the logs or using:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Note: I'm using PHPStorm and no errors are also showing up there.
Tired of this problem, it's taking quite a long time for what it is... really appreciate your help, I just want it to work.