Databases Reference
In-Depth Information
value='97.50' /></td>
<td><a href='edit.php?action=delete&gift_id=10'>Delete</a></td>
</tr>
Note that the whitespace we added to make the source code more readable doesn't
affect the formatting of the output. We've also shown each value starting on a new line
for readability.
After displaying all the existing gifts, we add a row of empty form input fields to the
table; Jack or Jill can enter gift attributes into these fields and submit the form to add
a new gift to the database. Since the gift_id field in our database is an auto-incremented
field that starts from 1, we can conveniently use the index 0 to identify fields for the
new gift. We could use a different name for the new gift fields—for example,
newgift_description , newgift_quantity , and so on, but we'd then need to add more
code to process these additional variables on the action page. Once we've printed this
last table row, we end the table, add a Submit button, and end the form. This ends our
showgiftsforedit( ) function:
...
echo "\n<table border='1'>";
...
// Display a row with blank form inputs to allow a gift to be added
echo "\n<tr><td></td>" .
"\n\t<td><input name='description[0]' size='60' /></td>".
"\n\t<td><input name='quantity[0]' /></td>".
"\n\t<td><input name='color[0]' /></td>".
"\n\t<td><input name='shop[0]' size='30' /></td>".
"\n\t<td><input name='price[0]' /></td>".
"\n</tr>";
// End the table
echo "\n</table>";
// Display a submit button and end the form.
echo "\n<input name='update' type='submit' value='Update data' />";
echo "</form>";
Deleting a Gift
If the user clicks on a Delete link for a form, the same edit.php script is called with the
query string action=delete&gift_id= the_gift_id ; the script must detect that some data
has been passed to it to process. As we mentioned earlier in “Logging Users In and
Out,” the query-string attribute and value pairs are available in the called script as
elements in the $_GET superglobal array. The edit.php script checks whether the $_GET
array contains any data; if it does, the script creates and executes an SQL query to delete
the corresponding gift from the database. Once the query has been executed, the pro-
gram proceeds to display the existing gifts for editing as before:
// See if we've arrived here after clicking the delete link
if(count($_GET) && (clean($_GET['action'], 10)=='delete'))
 
Search WWH ::




Custom Search