I am getting a mysqli_fetch_array() error when trying to execute an sql statement in one of my php forms. What it is doing is searching the database for the users email and returning a result. I can execute the SQL statement through dbforge and it works, however will not run when it is initiated via the web application..
Code:
<?php
// Start session
session_start();
// Include required functions file
require_once('includes/functions.inc.php');
require_once('includes/config.inc.php');
if (isset($_POST['email'])){
$email = $_POST['email'];
} else $email ="";
var_dump($email);
// Connect to database
$mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($mysqli, "SELECT
checkin.user,
SUM(checkin.points) AS point_total,
satellite_members.f_name,
satellite_members.l_name,
satellite_members.email
FROM checkin
INNER JOIN satellite_members
ON satellite_members.email = checkin.user
WHERE checkin.user = $email");
?>
HTML elements here:
<form action="" method="post">
<div class="form-group">
<label for="Email Address">Email Address</label>
<input type="email" class="form-control" id="email" name="email" style="width:60%; display:inline;" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<div class="table-responsive" style="width:100%; ">
<table class="table table-condensed table-bordered" >
<tr class="bg-cotu">
<th style="width:45%;" class="text-center">Member name</th>
<th style="width:20%;" class="text-center">Point Total</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['f_name']." ".$row['l_name'] . "</td>";
echo "<td>" . $row['point_total'] . "</td>";
echo "</tr>";
}
?>
</table>
</div>
mysql_real_escape_string($_POST['email'])should bemysqli_real_escape_string($mysqli,$_POST['email'])and quoting$email- Use error reporting which would've signaled those errors.mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation to accomplish this because you will create severe SQL injection bugs. Do not manually escape and inject strings.