简体   繁体   中英

show data from database in table format inside form

I have an input form connected 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:

  1. Print HTML table header
  2. Establish a connection to the database
  3. Issue a query, and capture the result (eg as an array)
  4. Loop through the array, printing each HTML table row
  5. Clean up database objects that may be holding onto memory or db connections
  6. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM