admin page for our gallery

Simple admin page to delete unwanted entries from a database.

Having an image gallery where users can upload anything they like is all very well if you can trust your users. But let’s be honest, it’s on the internet. You can’t trust anyone. They’re all after your credit card details. Probably. So it’s a good idea to create some sort of admin page which will allow you to delete any images you deem inappropriate.

Delete Record
A lot of the work for this has already been achieved when building the ‘gallery.php’ page, so we’ll use that as the basis for out admin page. Create a copy of ‘gallery.php’ and save it as ‘gallery-admin.php’. Keep it open and create a new page called ‘gallery-delete.php’. Strip it complete of code and then using the Server Behaviors tab, add a ‘Delete Record’ behavior.

Fill in the details as in the image below.

Delete Record set up

You’re basically just telling Dreamweaver which row of which table of which database to get rid of. So the database is defined as per our existing myDB connection, and the table will be the images table. It’s the primary key column that ensures we select the correct image (if you remember, the primary key column ‘id’ is always unique) and we will be passing information about which id to select via a URL parameter also called ‘id’ (although this name can actually be anything you like). We’re setting the scene so that when you browse to ‘delete.php’ in your browser, if you append to the URL a string of ‘?id=1′ for example, it’ll select and delete whichever image matches id 1. Including that string in the URL itself means that we can retrieve the variable of ‘id’ on our page.

You can pass any variables in this manner. Using the URL of ‘test.php?name=matt&home=plymouth’ for instance will pass variables of name and home to the test page which you can then retrieve within the page using $_GET[’name’]. This is a fairly easy way of defining variables but it’s very unsecure and shouldn’t be used for any sensitive information (like passwords) because the variable value is clearly visible in the URL.

I digress.

That’s the only code we need to add to the ‘gallery-delete’ page so save that and upload it ready for use. Then return to your ‘gallery-admin’ page and somewhere within the repeat region (which should be outlined in design view), add some text like ‘Delete this image’.

Delete this image link

Highlight the text you just entered and using the properties panel in Dreamweaver, add a link using the Browse for File icon. Browse to and select the ‘gallery-delete.php’ page you created earlier and then, before you press ‘choose’, click on the Parameters.. button.

Browse to file and add parameters

This will allow you to append the URL with the id of each image dynamically. Remember, this is how the delete code will determine which image to actually drop. So set the parameter name to ‘id’ (this is what the URL string will start with.. i.e. ‘?id=’) and then use the lightning icon to get the value from a data source, and select the ‘id’ from the getpics recordset.

Dynamic id

You should then have a link which consists of the path to your ‘gallery-delete’ page, appended by a fairly long looking string. When you come to view this page, that string will only consist of ‘?id=’ a single number relative to each individual image.

Final link path

Now save your page, upload it and browse to it. All images should now have the ‘Delete this image’ link next to them, and if you click on the link, that image will disappear!

Get rid of the filth!

NOTE: This is irreversible so make sure you want to delete the entry from your database before you click on the link. In a practical situation, it would be best to have some sort of confirmation page. Also, this page is not privy to any security restrictions at the moment - so anyone can browse to it and delete images at will. We’ll come to access privileges later on.

Stuff.