Im building a website and im trying to avoid mysql injection, so im changing all the queries and connections.
I have the connection os a separated file, called ‘functions.php’:
$mysqli = new mysqli("localhost", "root", "mysql", "padelbeat");
if($mysqli->connect_error) {
exit('Error connecting to database'); //Should be a message a typical user could understand in production
}
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli->set_charset("utf8mb4");
function createIdentifier(){
date_default_timezone_set('Europe/Lisbon');
date_default_timezone_get();
$fileName = date("Ymdhis");
return $fileName;
}
And this is the file that does not work at all:
In the case below, it is not returning any values:
<?php
if(!isset( $_SESSION['user']['userID'])){
print "<script>top.location = '../index.php?id=5';</script>";
exit();
} else if($_SESSION['user']['role'] != "customer" ){
print "<script>top.location = '../index.php?id=5';</script>";
exit();
}
include '../../includes/functions.php';
$userPoints = $row_card['card_points'];
$stmt_sel_prods = $mysqli->prepare("SELECT * FROM users WHERE prod_state = ?");
$stmt_sel_prods->bind_param("s", 1);
$stmt_sel_prods->execute();
$result_sel_prods = $stmt_sel_prods->get_result();
if($result_sel_prods->num_rows != 0){
while($row_sel_prods = $result_sel_prods->fetch_assoc()) {
extract($row_sel_prods);
$percentageReachProd = ($userPoints * 1 / $prod_points) * 100;
echo "
<div class='product'>
<div class='prod_img'><img src='../../assets/products/$prod_img' width='150px'></div>
<div class='prod_title'>$prod_name</div>
<div class='prod_desc'>$prod_desc</div>
<progress id='progressBar' max='100' value='$percentageReachProd' style='margin-bottom:5%; margin-top:-5%'></progress>
<div class='prod_seemore'><button class='btn_seemore' onclick='location.href="index.php?page=21&prodID=$prod_id";' >Trocar por $prod_points <img src='../../assets/default/beat.png' width='11px'></button></div>
</div>
";
}
}else{
echo " <tr>
<td colspan='8'>Desculpe, mas não existem registos...</td>
</tr> ";
}
$stmt_sel_prods->close();
?>
But in similar ones it does work.
I’ve tried to change the query and it does not work.
I’ve also tried to do the connection in the same page, and it also does not work.
Can someone tell me what is wrong with this block of code?
Thank you all