I am trying to get my SQL table to populate into an HTML table via PHP. However, I just get the table header created. What's wrong with the populating? Here's my code:
<?php
$con=mysqli_connect("server.com","username","password");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Orders");
echo "<table>";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>orderNumber</th>
<th>Price</th>
<th>customerName</th>
<th>salesRep</th>
<th>DatePicker</th>
<th>shipMethod</th>
<th>trackingNumber</th>
<th>Statuscheck</th>
<th>Edit</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['orderNumber'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['customerName'] . "</td>";
echo "<td>" . $row['salesRep'] . "</td>";
echo "<td>" . $row['DatePicker'] . "</td>";
echo "<td>" . $row['shipMethod'] . "</td>";
echo "<td>" . $row['trackingNumber'] . "</td>";
echo "<td>" . $row['Statuscheck'] . "</td>";
echo "<td>" . $row['Edit'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
The 'Edit' is so i can have an edit hyperlink to use SQL UPDATE. It is not actually part of the SQL table in the database. Any help is greatly appreciated. Thanks!
OK, so here is the problem now, the edit page doesn't prepopulate the fields that have already been filled out, nor does the update button actually update the db. Here is the code for the edit page.
$query = "SELECT orderNumber, customerName, salesRep, DatePicker, shipMethod, trackingNumber, StatusCheck FROM Orders WHERE id = '$id'";
$result = @mysql_query($query);
mysql_fetch_object($result);
?>
<form name="update order" method="post" action="edit.php?a=edit&id=<? echo($ID) ?>&update=1">
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%">Order Number</td>
<td><input name="orderNumber" type="text" id="orderNumber" value="<? echo($row->orderNumber) ?>"></td>
</tr>
<tr>
<td>Customer Name</td>
<td><input name="customerName" type="text" id="customerName" value="<? echo($row->customerName) ?>"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Sales Rep</td>
<td><input name="headline" type="text" id="headline" value="<? echo($row->headline) ?>"></td>
<tr>
<td>Must Ship By</td>
<td><input name="DatePicker" type="text" id="DatePicker" value="<? echo($row->DatePicker) ?>"></td>
<tr>
<td>Shipping Method</td>
<td><input name="shipMethod" type="text" id="shipMethod" value="<? echo($row->shipMethod) ?>"></td>
<tr>
<td>Tracking Number</td>
<td><input name="trackingNumber" type="text" id="trackingNumber" value="<? echo($row->trackingNumber) ?>"></td>
</tr>
<tr>
<td>Status</td>
<td><input name="StatusCheck" type="radio" name="status" value="PROCESSING"> PROCESSING<br><input name="StatusCheck" type="radio" name="status" value="PROCESSING"> PICKED<br><input name="StatusCheck" type="radio" name="status" value="PROCESSING" value="<? echo($row->StatusCheck) ?>"> SHIPPED<br> value="<? echo($row->StatusCheck) ?>"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="hiddenField" type="hidden" value="update">
<input name="add" type="submit" id="add" value="Update">
</div></td>
</tr>
</table>
</form>
<?php
?>
Your
$result
variable is empty, test it out. The reason why is because you did not set up any database name where you can get information from. Paste this:after
and it should be okay (of course if you have any records there).
Your code aboove is kindof lengthy so, i'm going to take a smaller example. lets assume you have a record of id and name. to create the edit/update links change the while loop to some thing similar to the following
this will append an edit link to all the rows. and create the editform.php as follows:
you can handle the update on the same page too, and i recommend that. Also you can create an edit form using JavaScript but comes with some complexity.