I'm trying to retrieve a table from MySql database, but my code returns incorrect data. here is my code below
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "net_trade";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno()) {
die("Connection failed: " . mysqli_connect_error());
}
echo "<link rel='stylesheet' type='text/css' href='style.css'>";
echo "<h1>NET TRADE</h1>";
echo "<div align='center'>";
echo "<ul>";
echo "<li><a class='nav' href='inventory.php'>INVENTORY</a></li>";
echo "<li><a class='nav' href='supplier.php'>SUPPLIER</a></li>";
echo "<li><a class='nav' href='customer.php'>CUSTOMER</a></li>";
echo "<li><a class='nav' href='sales.php'>SALES</a></li>";
echo "</ul>";
echo "</div>";
echo "<form method='POST' action='create_supplier.html'>
<input type='SUBMIT' class='style19' name='new_item' value='Add New Item'></form>";
echo "<table class='TFtable'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Quantity</th>";
echo "<th>Name</th>";
echo "<th>Brand</th>";
echo "<th>Model</th>";
echo "<th>Serial</th>";
echo "<th>Date Supplied</th>";
echo "<th>Supplier</th>";
echo "</tr>";
$result = mysqli_query($conn, "SELECT * FROM inventory");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo "<tr>";
echo "<td>".$row['0']."</td>";
echo "<td>".$row['1']."</td>";
echo "<td><a href='edit.php?id=".$row['0'].">".$row['2']."</a></td>";
echo "<td>".$row['3']."</td>";
echo "<td>".$row['4']."</td>";
echo "<td>".$row['5']."</td>";
echo "<td>".$row['6']."</td>";
echo "<td>".$row['7']."</td>";
echo "</tr>";
}
mysqli_free_result($result);
}else {
echo "0 results";
}
mysqli_close($conn);
echo "</table>";
?>
the result should be this...
but my code returns something like this..
the result skips a row from the database table.. thats my main prob
I believe this line is goofed up in your code.
echo "<td><a href='edit.php?id=".$row['0'].">".$row['2']."</a></td>";
You need, noticing the matched double quotes in the href, something this this.
<td><a href="edit.php?id=12345">asd</a></td>
What you're getting, I think, is this.
<td><a href='edit.php?id=12345>asd</a></td>
Notice your href is started with a single quote (which web browsers tolerate, but is non standard), and isn't ended.
You probably want code looking something like this. String concatenation is not easy to read, so sprintf()
can be superior for making up this kind of link tag.
echo sprintf ('<td><a href="edit.php?id=%d>%s</a></td>', $row[0], $row[2]);
Pro tip: Always do view source when your results are bizarre.