Code for Deleting PHP Photo Gallery Photo
So why might anyone want to learn to make a PHP photo gallery when there are plenty of freebies out there and in control panels of websites? Because learning is fun, and you might try using this as a stepping stone for writing your own PHP gallery script. And if you don't want to learn PHP, we have a JavaScript photo gallery that we teach you about. These scripts are fun to tinker with, modify, and learn from.
For more on viewing and creating a PHP photo gallery, simply go to code-for-php-photo-gallery.html
Here are the tutorial files for all the photo gallery functions:
View Gallery
Create Gallery
Add Category
Delete Category
Add Photo
Delete Photo
So, on to the code. We define the CSS, then run browser sniffers and finetune the display with the fix() function, run by the onload in the body tag. Next, there's the PHP. The PHP code starts as so many PHP codes start: by getting the db connection data from a config file. It shows the password, username, and database name. Then we POST the photo to delete, and do input filtering since the only allowed characters are numbers, letters, underscore, dot, and hyphen. The page's instructions say: "To delete a photo, copy it from your file manager and put it into the input box, or simply type it in. THIS WILL NOT DELETE THE ACTUAL PHOTO—JUST ITS NAME IN THE DB."
Next we search the available photos in the MySQL db table photos—this table is read and if the photo is not found, a message informs the user of this. If it is found, it's deleted. The HTML form's action is "cms-edit-photo-gallery-delete-photo.php"—the page reloads itself to do the POSTing.
TERMS OF USE: You must put a link on your site's home page to:
<a HREF="http://www.css-resources.com">css-resources.com</a> and it must NOT be a "nofollow" link, now or ever, and the link must stay live and unbroken for as long as you use the photo gallery script you'll find on this page, and you may not edit any part of the link, which must say "css-resources.com". Your site must NOT, now or ever, be about violence or hate or crime or treason or porn or terrorism, and your photo gallery script site/page must not have photos involving violence or hate or crime or treason or porn or terrorism. Do not use the sample photos in our sample JavaScript photo gallery for anything: they're copyrighted by © Focus Multimedia Ltd., Serif (Europe) Ltd., and Hemera Technologies Inc. If you cannot accept these terms, please do not use our script. We reserve the right to deny our script use to anyone with an unacceptable website. Our script is copyrighted by us, at MCS Investments, Inc. All these codes work—we have a working version we use here using this same code.
SAVE THIS PAGE AS: cms-edit-photo-gallery-delete-photo.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Editing Photo Gallery—Content Management System (CMS)—Delete Photo</TITLE>
<meta name="description" content="Editing Photo Gallery—Content Management System (CMS)—Delete Photo">
<meta name="keywords" content="Delete photo,Editing photo gallery,Content Management System,Content Management System Articles,php,CMS,javascript, dhtml, DHTML">
<script language="javascript">
var cat=new Array();
mactest=(navigator.userAgent.indexOf("Mac")!=-1) //My browser sniffers
is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1
Netscape=(navigator.appName.indexOf("Netscape") != -1)
msafari=(navigator.userAgent.indexOf("Safari")!= -1)
wsafari=0; if(!mactest&&msafari){wsafari=1;msafari=0}
is_opera = 0; if(window.opera){is_opera=1}
is_ie_mac = 0; is_ie=0;if(document.all){is_ie=1}
if(is_ie&&mactest){is_ie_mac=1}
function fix(){if(Netscape||is_opera){e=document.getElementById('top');e.style.marginTop='1px';e=document.getElementById('info');e.style.marginTop='1px';}}
</script>
<style type="text/css">
BODY {margin-left:0; margin-right:0; margin-top:0;text-align:left;background-color:#ddd}
p, li {font:13px Verdana; color:black;text-align:left}
h1 {font:bold 20px Verdana; color:black;text-align:center}
h2 {font:bold 24px Verdana;text-align:center}
h3 {font:bold 11px Verdana;}
.photo {position:absolute;top:0px;left:10px;width:989px}
.photos {position:absolute;top:300px;left:10px;width:989px}
.form2 {position:absolute;top:240px;left:215px;width:700px}
.info {position:absolute;top:80px;left:200px;width:700px;border:1px solid blue;padding:6px;background-color:#bbb}
.options {width:700px;padding:6px;border:1px solid blue;background-color:#bbb}
</style>
</head>
<body onload='fix()'>
<?php
include_once"config.php";
$pattern2 = '/[^a-zA-Z0-9\\.\\-\\_]/i';
$replacement = '';
$P=$_POST['photo'];
$P=strip_tags($P);
$P=preg_replace($pattern2, $replacement, $P);
if (strlen($P)>3){
$check_user_data = mysql_query("SELECT photo FROM photos WHERE photo='$P'") or die(mysql_error());
if(mysql_num_rows($check_user_data) == 0)
{echo '<script language="javascript">alert("This photo is not in your DB. Please try again.")</script>;';
}else{
mysql_query("DELETE FROM photos WHERE photo='$P'") or die('Error ,deleting failed');
$rc = mysql_affected_rows();
if ($rc>0){echo '<script language="javascript">alert("The deleting was successful.");window.location = "cms-edit-photo-gallery-delete-photo.php";</script>';}
else{echo '<script language="javascript">alert("The deleting was unsuccessful.");</script>';}
}}
unset($P);
mysql_close();
?>
<div id='top' class='photo'>
<h1>Editing Photo Gallery—Content Management System (CMS)—Delete Photo</h1>
<div id='info' class='info'><h3>To delete a photo, copy it from your file manager and put it into the input box, or simply type it in.<br>THIS WILL NOT DELETE THE ACTUAL PHOTO—JUST ITS NAME IN THE DB.</h3></div>
</div>
<div class='photos'>
<form method="post" action="cms-edit-photo-gallery-delete-photo.php">
<table width="700" border="0" cellpadding="2" cellspacing="2" align="center">
<tr>
<td width="130">Photo</td>
<td width='570'><input name="photo" type="text" size="100"></td>
</tr>
<tr>
<td> </td><td><input name="retrieve" type="submit" value="Delete Photo in DB"> <br><br>
<input name="reset" type="reset" value="Reset"></td>
</tr>
</table>
</form>
</div>
<?php include("navi.html"); ?>
</body>
</html>