In this tutorial we will cover the following:
PHP and Dreamweaver MX Continued
Editing and Updating information
OK, so we can create new records and delete existing ones as we saw by creating the Great Britain record and the removing the UK one. But that's hardly the way to alter information in our database. So let's create a page that we can use to edit our information. Create a new PHP page and save it as edit.php. Now let's link to that page from our countries.php page. Exactly as we did with the Delete link, we will create a link to edit.php, passing the value of the country_pk field as the country_pk URL paramter. Your link should be
edit.php?country_pk=<?php echo $row_rsCountries['country_pk']; ?>
Save countries.php and switch back to edit.php.
The first thing we will need to do is retrieve the database record. Create a new recordset named rsCountry. Again, select all the fields, but this time we only want a single record, identified by the primary key. We know that the page will have received the value as a URL parameter called country_pk as we just set that link up in our countries.php page. Select to Filter where country_pk equals the URL paramter country_pk:
Click OK and the recordset is created. We can now create our form to display
the data. Create the exact same form as we did for inserting new records. This
however we will need to add a hidden field that stores the primary key of the
current record. We will need this so that we know what record to update later. Call
the hidden field country_pk. Now we need to fill the form with the values that
we retrieved from the database. Click on each field in turn and then click
the button next to the Init Val box select the correct
field from the rsCountry recordset. The text box country_code will have the
value of the country_code field, the hidden country_pk input will have the value
of country_pk and the country_name will have the value of the country_name field
from the recordset.
Now we can add the Update Record Server Behavior.
Our values will come from our form, form1, we'll be using the dmxzone connection and updating the table countries. Because of the way that we named our form fields, Dreamweaver will automatically know that we are updating the record that is selected by the primary key country_pk and that which form field corresponds with which field in the database. After updating we will go back to countries.php./p>
Click OK and you can test the page. Click on one of the Edit links, alter the country name and then click on Update. You will be redirected back to the countries.php page with the updated information displayed.
Allan Kent
Allan comes from Cape Town, South Africa. He has been implicated in writing for several WROX, glasshaus, Wiley and Apress publications, generally in the 'cool stuff that PHP can do' sections.
You can catch up with him at his website http://www.mediafrenzy.co.za.
Comments
upload image
is there also a way to input (upload) an image within this script and upload the file to a "var/www/home/img" folder?. And if you open the data is is associated with that particular data.?
Thanks, george.
,RE: upload image
Hi George,
there's an excellent extension on DMXZone called Pure PHP Upload which sounds like it will sort you out.
http://www.dmxzone.com/ShowDetail.asp?NewsId=4509
Allan
Problem with edit page
Hi,
I tried this tutorial, but I'm having some problems. After I've clicked the edit link, I don't see the values that where associated with the primary key, instead I always see the first data in my table. But when I put my mouse over the EDIT link, I do see the correct Primary key. It seems that the edit page does'nt receive the PK.
My recordset is set with get PK from URL, what else should I verify ?
Thank you,
RE: Problem with edit page
You must me logged in to write a comment.