I am trying to display all recipes belonging to a registered user using a while loop. I noticed that let them say that the user has 4 recipes, the first of them will not be displayed, which will lead to the appearance of only 3 recipes in the table. If the user has 3 recipes, the first of them will not be displayed, as a result, only 2 recipes are displayed, etc.
I did my research and found out that this is because the first line will be ignored and therefore not displayed.
Is there anyone who can suggest what kind of corrections to make to the cycle to fix this problem? My codes include display in a table, so I still can’t figure out what should be done, despite the other questions that have already been sent and answered.
Many thanks!
<?php
$userid = $_SESSION['userid'];
$mysqli = new mysqli("localhost", "root", null, "recipedb") or exit("Error connecting to database");
$stmt = $mysqli->prepare("Select recipename,recipeid,imagefile from recipe where userid=?");
$stmt->bind_param("s", $userid);
$stmt->execute();
$stmt->bind_result($recipename, $recipeid, $imagefile);
if ($stmt->fetch() == null) {
echo "You did not have any recipes yet.<br />";
}
else {
echo "<table style=width:100% >";
echo "<tr><td><b>Recipe</b></td><td><b>Actions</b></td></tr>";
while ($stmt->fetch()) {
echo "<tr>";
echo "<td><b>$recipename</b><br /><b>Recipe ID:</b>$recipeid<br /> <img src='images/$imagefile' height='125' width='125' > </td>";
echo "<td> <a href='recipedetails.php?recipeid=$recipeid'>View</a> ";
echo "<a href='deleteconfirmation.php?recipeid=$recipeid'>Delete</a> ";
echo "</tr>";
}
echo "</table>";
}
$stmt->close();
$mysqli->close();
?>
source
share