Databases Reference
In-Depth Information
Once we've created the table, we run an SQL query to list all the gifts in the gifts table,
and order the results by the alphabetical order of the gift descriptions. If no gifts are
found, we display a suitable message; if any gifts are found, we display them for editing:
// Create an SQL query to list the gifts in the database
$query = "SELECT * FROM gifts ORDER BY description";
// Run the query through the connection
if (($result = @ mysqli_query($connection, $query))==FALSE)
showerror($connection);
// Check whether we found any gifts
if(!mysqli_num_rows($result))
// No; display a notice
echo "\n\t<tr><td colspan='7' align='center'>".
"There are no gifts in the database</td></tr>";
else
// Display the results for editing...
To display the results, we execute a while( ) loop that repeatedly calls the
mysqli_fetch_array( ) function to fetch the results a row at a time as an associative
array. We assign this array to the $row variable, allowing us to access the result fields
by using the field name as the array index. For example, we can access the descrip
tion field data for a gift row as $row["description"] . When no more rows are available,
the function returns FALSE ; when our while loop encounters this value, it ends the loop.
For each gift item, we compose an HTML table row with form input fields, and prefill
the fields with the gift data from the database. We have multiple gifts, each with a
description, quantity, and so on; we could create the HTML as below:
<tr>
<td>6</td>
<td><input name='description' value='Avanti Twin Wall Mixing Bowls 2.8 Ltr'
size='60' /></td>
<td><input name='quantity' value='2' /></td>
<td><input name='color' value='Silver' /></td>
<td><input name='shop' value='Myer' size='30' /></td>
<td><input name='price' value='41.65ea (83.30 total)' /></td>
</tr>
<tr>
<td>10</td>
<td><input name='description' value='Baileys Comet 6 Ladder' size='60' /></td>
<td><input name='quantity' value='1' /></td>
<td><input name='color' value='Silver' /></td>
<td><input name='shop' value='Bunnings' size='30' /></td>
<td><input name='price' value='97.50' /></td>
</tr>
This form contains as many description , quantity , color , shop , and price fields as there
are gifts. However, we can access only one set through $_POST["description"] ,
$_POST["quantity"] , and so on. An easy way to resolve this problem is to name the form
fields as arrays, with the ID of each gift used as the index of the array. Thus, for example,
the form input for the description of gift 6 is named description[6] .
 
Search WWH ::




Custom Search