All the Examples on this page can be found in the php sample site
Choosing the Pet to Update
In this simple database example, if we want to edit a particular pet, we must first choose that pet. In this example, we will make our box a multi-list box of all pets in the database instead of a pulldown. You can switch back and forth easily. Both styles are populated the same way. For more on populating combination boxes, see the Combo Boxes Page.
Some PHP code required in order to retrieve the list of Pets:
In order to actually get the form to pull up that pet's information, choose the pet, click the "EDIT" submit button and the form will got to the next Update Pet Page, pupdate2.php, which actually contains the form to update the pet. To do this, we make the form's action the next Update Pet Page:
Displaying the Form with the Pet Information:
The Form is very similar to the Add a New Pet Form except there are "values" inside the text area of each field:
The form will call pupdate3.php which will perform the actual database update. Note that the table column "petid" is passed hidden. This is to ensure that the next page will be able to use it even though it is not displayed in the form and the user never sees it.
If you look, you'll notice that the form to add a pet does not have the hidden field petid. Why? Because the petid is determined when the record is actually inserted into the database, not by the user :) (cool!).
Updating the Database
We again connect to the server and open the database. Pupdate3.php is clean and simple, showing only the text box containing the php code to update the table and links.
If the Pet name is not blank, we can update the information in the table.
Converting integer strings: HTML form text fields have a tendency to convert non-text values to "text" datatypes. Saving "text" datatypes into a MySQL column that should contain numeric data will cause the SQL statement to fail. The reason for the datatype conversion may be server based, database based, or a PHP version issue. The issue seems to be intermittant, based on PHP configurations, MySQL database setup, and operating system the application is running under, but it does occur often enough to be a nuisance. In order to prevent running into this, it is a good idea to perform a SETTYPE on any integer, logical, or Double datatypes that have been editted via an HTML form before inserting or updating into the database.
next lesson - Deleting Records
|