I have the following code and this has been taking up my time lately. I got two databases, one called rewards and the other called membership and I like to find users who have got more of a particular points and who has a null membership. Must I use inner join? I am trying to do the following but there is a problem with my $resultCheck3 and that it is not picking up any records, where there is one record:
<?php
ob_start();
include_once __DIR__.'/header2.php';
if(!isset($_SESSION['u_uid'])) {
echo "<meta http-equiv='refresh' content='0;url=index.php?level3promo=notlogin'>";
exit();
} else {
if($_SESSION['u_permission'] == 0) {
echo "<meta http-equiv='refresh' content='0;url=header2.php?level3promo=nopermission'>";
exit();
} else {
include_once __DIR__. '/includes/dbh.php';
$category = strip_tags($_POST['category']);
$csrf = strip_tags($_POST['csrf']);
if(!isset($_SESSION['key'])) {
echo "<meta http-equiv='refresh' content='0;url=header2.php?level3promo=notset'>";
exit();
} else {
if($csrf !== $_SESSION['key']) {
echo "<meta http-equiv='refresh' content='0;url=header2.php?level3promo=errorwithform'>";
exit();
} else {
$sql = "SELECT * FROM level3promo WHERE id = ?;";
$stmt = mysqli_stmt_init($conn);
//Prepare the prepared statement
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'SQL statement failed';
} else {
//Bind parameters to the placeholder
mysqli_stmt_bind_param($stmt, "i", $category);
//Run parameters inside database
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck < 1) {
echo "<meta http-equiv='refresh' content='0;url=header2.php?level3promo=nocategories
exit();'>";
} else {
$row = mysqli_fetch_assoc($result);
$sql2 = "SELECT * FROM rewards WHERE reward_points >= ?;";
$stmt = mysqli_stmt_init($conn);
//Prepare the prepared statement
if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo 'SQL statement failed';
} else {
//Bind parameters to the placeholder
mysqli_stmt_bind_param($stmt, "i", $row['points']);
//Run parameters inside database
mysqli_stmt_execute($stmt);
$result2 = mysqli_stmt_get_result($stmt);
$resultCheck2 = mysqli_num_rows($result2);
echo '<table class="admin_level3promo">
<tr>
<th colspan="2" class="update_title">Welcome to the Administrator\'s Level 3 Promo Qualified Users\' Section</th>
</tr>';
if ($resultCheck2 < 1) {
echo "<meta http-equiv='refresh' content='0;url=header2.php?level3promo=lackofpoints'>";
exit();
} else {
$rowCounter = 0;
while($row2 = mysqli_fetch_assoc($result2)) {
$sql3 = "SELECT * FROM memberships WHERE user_uid = ? AND subscriptionplan3 IS NULL;";
$stmt = mysqli_stmt_init($conn);
//Prepare the prepared statement
if (!mysqli_stmt_prepare($stmt, $sql3)) {
echo 'SQL statement failed';
} else {
//Bind parameters to the placeholder
mysqli_stmt_bind_param($stmt, "s", $row2['user_uid']);
//Run parameters inside database
mysqli_stmt_execute($stmt);
$result3 = mysqli_stmt_get_result($stmt);
$resultCheck3 = mysqli_num_rows($result3);
if ($resultCheck3 < 1) {
echo "<meta http-equiv='refresh' content='0;url=header2.php?level3promo=lackofrecords'>";
exit();
} else {
while($row3 =mysqli_fetch_assoc)
echo '<tr>
<th>Result No:</th><td>',htmlspecialchars($rowCounter),'</td>
</tr>
<tr>
<th>ID:</th><td>',htmlspecialchars($row2['id']),'</td>
</tr>
<tr>
<th>Username:</th><td>',htmlspecialchars($row2['user_uid']),'</td>
</tr>
<tr>
<th>E-Mail:</th><td>',htmlspecialchars($row2['user_email']),'</td>
</tr>';
}
echo '</table>';
$rowCounter++;
}
}
}
}
}
}
}
}
}
}
ob_end_flush();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
</body>