FGOD FGOD - 1 month ago 11
PHP Question

results doesn' t show all entries

I'm working on a script that should show a list of my database entries so i can edit or delete them, but for some reason it only shows 4 out of 5 results from the database. If I enter a new entry into the database, it does show 5 results, but still not the 1 it didn't show before. This is pretty weird, cause my first try at making such a script did show all results. And I can't seem to find out what is wrong with it.

<?php

session_start();
include("important/passwords.php");
check_logged();

session_start();

include_once("includes/config.php");

$result = mysqli_query($mysqli, "SELECT * FROM news ORDER BY id");

$count=mysqli_num_rows($result);
while($rows=mysqli_fetch_array($result)){

?>

<html>
<head>
<title>Homepage</title>
<link rel="stylesheet" type="text/css" href="style2.css">
</head>

<body>
<div class="divborder" style="width:600;">

<table width='80%' border=0 bgcolor="#CCCCCC" bordercolordark="#666666" class="table">
<tr bgcolor="#DFDFDF">
<td>Datum</td>
<td>Nieuws</td>
<td>Update</td>
</tr>
<?php
while($res = mysqli_fetch_array($result)) {
echo "<tr bgcolor='#FFFFFF'>";
echo "<td bgcolor='#FFFFFF'>".$res['date']."</td>";
echo "<td bgcolor='#FFFFFF'>".$res['news']."</td>";
echo "<td bgcolor='#FFFFFF'><a href=\"edit.php?id=$res[id]\">Aanpassen</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Weet je zeker dat je dit nieuwsbericht wil verwijderen?')\">Verwijderen</a></td>";
}
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><form action="newsupload.php"><input name="update" type="submit" id="update" value="Voeg nieuws toe" class="upload"></form></td>
</tr>

</table>
</div>
</body>
</html>


This is the code i'm having that should make a new row in the table for every entry that is found. I don't know if it might have something to do with the fact that there are 2 messages without date entry in database, but on my previous try to get this working, it hadn't had any problems with showing both entries without were date is NULL.

Answer

Change you code while looping same condition two bar remove one while loop

session_start(); 
include("important/passwords.php"); 
check_logged(); 

session_start();

include_once("includes/config.php");

$result = mysqli_query($mysqli, "SELECT * FROM news ORDER BY id"); 

$count=mysqli_num_rows($result);


?>

<html>
<head>    
    <title>Homepage</title>
    <link rel="stylesheet" type="text/css" href="style2.css">
</head>

<body>
<div class="divborder" style="width:600;">

    <table width='80%' border=0 bgcolor="#CCCCCC" bordercolordark="#666666" class="table">
        <tr bgcolor="#DFDFDF">
            <td>Datum</td>
            <td>Nieuws</td>
            <td>Update</td>
        </tr>
        <?php 
        while($res = mysqli_fetch_array($result)) {         
            echo "<tr bgcolor='#FFFFFF'>";
            echo "<td bgcolor='#FFFFFF'>".$res['date']."</td>";
            echo "<td bgcolor='#FFFFFF'>".$res['news']."</td>";    
            echo "<td bgcolor='#FFFFFF'><a href=\"edit.php?id=$res[id]\">Aanpassen</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Weet je zeker dat je dit nieuwsbericht wil verwijderen?')\">Verwijderen</a></td>";        
        }
         ?>
                              <tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><form action="newsupload.php"><input name="update" type="submit" id="update" value="Voeg nieuws toe" class="upload"></form></td>
</tr>

    </table>
    </div>
</body>
</html>