This question already has an answer here:
I have a script which runs, then directs to a page to delete a record. I want to set it so when the record count is zero, the page redirects to the index.php page. Code as follows:
$sql="SELECT * FROM fbi_with_invoice";
if ($result=mysqli_query($conn,$sql))
{
$rowcount=mysqli_num_rows($result);
printf("There are %d rows remaining.
",$rowcount);
}
if ($rowcount=0) {
header('refresh:5;url=index.php');
mysqli_free_result($result);
}
$sql = "DELETE FROM fbi_with_invoice ORDER BY personid DESC LIMIT 1";
if ($conn->query($sql) === TRUE) {
header('refresh:5;url=index2.php');
}
$conn->close();
Currently $rowcount
gives 0, however the page redirects to index2.php.
</div>
The problem is here: if ($rowcount = 0)
you used the assignment operator instead of ==
comparison operator.
$sql="SELECT * FROM fbi_with_invoice";
if ($result=mysqli_query($conn,$sql))
{
$rowcount=mysqli_num_rows($result);
printf("There are %d rows remaining.
",$rowcount);
}
if ($rowcount == 0) {
header('refresh:5;url=index.php');
mysqli_free_result($result);
}
$sql = "DELETE FROM fbi_with_invoice";
if ($conn->query($sql) === TRUE && $rowcount) {
header('refresh:5;url=index2.php');
}
$conn->close();