PHP and Dreamweaver MX Continued

In this tutorial we will cover the following:

  • Creating Recordsets
  • Displaying Data
  • Creating New Records
  • Deleting Records
  • Editing and Updating Records
  • Creating new records

    In order to put data into the database, we will first need a form. Create a form with two text fields. You can save yourself some effort later down the line by naming the text fields the same as the corresponding field in the database. The two pieces of information we will need to create a new record is the country code and the country name. These are stored in the countries table in the fields country_code and country_name, so name the text fields country_code and country_name.  When we come to inserting the data, Dreamweaver will automatically match the text boxes with their corresponding field names.

    My first text box called country_code has a char width of 2 and max characters set to 2.  The second text box is called country_name.  I have also added a Check Form Behavior and made both text boxes required.  Now add an Insert Record Server Behavior:

    Select the dmxzone connection, the countries table and you will notice that Dreamweaver matches up the text boxes with their corresponding field names.  After inserting we want to go back to countries.php.

    You can now save the page and test it out in a browser.

    Deleting records

    As you can see from the screenshot above, we've decided to add in Great Britain and so we should now remove United Kingdom. Create a new Dynamic PHP page and save it as delete.php. Go ahead and add a Delete Record Server Behavior. The Delete Record dialog is displayed for you to provide all of the relevant information

    The first thing that the behavior will do is check to see whether a specific variable is set  if this variable is set then the Delete behavior is triggered. You can set this to a specific server variable, URL parameter, cookie or form value. Our Delete behavior will be triggered if the primary key value for the table is passed to the page.  We'll see how to do this in just a bit.  We then have to specify the database connection and table that we are deleting from.  The primary key column is the column that we will use to specify the record to delete.  We also have to tell the behavior how to find the primary key value  in our case it will be coming from a URL parameter called country_pk. After we have deleted the record we can go back to countries.php.

    Once you have inserted the behavior save the page and switch back to countries.php. We now need to create a link that will call the delete.php page and trigger the delete behavior.  Select the text Delete that we added earlier and then in the Properties panel click the Browse for File button next to the Link box

    Select the delete.php page as this is the page that we have created our Delete Record Server Behavior in, and then click the Parameters button so that we can create the country_pk URL parameter that will trigger the Delete behavior.

    Click the  button in the Value column of the Parameters dialog to select the country_pk field from the rsCountries recordset.  Click OK and OK again to add the link.  The link text should be:

    delete.php?country_pk=<?php echo $row_rsCountries['country_pk']; ?>

    Save the page and view it in your browser.

    When you hover over the Delete link next to the United Kingdom you should see the URL in the status bar:

    http://localhost/dmxzone/countries.php?country_pk=1

    Clicking on the link will delete the record.

    Allan Kent

    Allan KentAllan 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.

    See All Postings From Allan Kent >>

    Comments

    upload image

    September 16, 2003 by thee soul

    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

    September 17, 2003 by Allan Kent

    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

    May 6, 2004 by lime limeo

    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

    May 7, 2004 by Allan Kent
    Hi, sounds like the filter for the rsCountry recordset isn't being set correctly and it is using the default of 1 for the country_pk - double check that you spelt the country_pk URL paramater correctly in the Edit link in countries.php as well as in the Recordset dialog for rsCountry in edit.php. Allan
    See all 12 Comments

    You must me logged in to write a comment.