I have an input form connected 开发者_Go百科to a database. After [the form is submitted], I want to make a form to show all the data which has been input to the database. I want to show this data in table sortable by name or date.
Please help me.
The high-level steps you want to take are:
- Print HTML table header
- Establish a connection to the database
- Issue a query, and capture the result (e.g. as an array)
- Loop through the array, printing each HTML table row
- Clean up database objects that may be holding onto memory or db connections
- Print HTML table close
The following example is a slightly modified version of example #2 from this page on php.net. I suggest you spend a lot of time on that site - the manual is excellent, and almost every page has numerous working examples in the comments section.
<table>
<?php
// Establish the database connection
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
// Issue the query
$result = mysql_query("SELECT id, name FROM mytable");
// Capture the result in an array, and loop through the array
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
// Print each row as HTML: <tr><td>row 0</td><td>row 1</td>
printf("<tr><td>%s</td><td>%s</td></tr>", $row[0], $row[1]);
}
// Free the result set
mysql_free_result($result);
?>
</table>
精彩评论