Edit Website Directories: Delete URL—Content Management System (CMS)
The cool thing about making website directories is that they can be very useful when you need to go somewhere on the Web but you're not sure of the name. We use our directory as a programmer's resource which is better than Favorites in a browser because you can get a thumbnail and description and other info dynamically just from hovering over a link, without having to click on the link and go there (which you can also do, of course).
You may, of course, choose to leave out the code about thumbnails and dynamic info boxes and just have clickable links for the categories and the URLs, or store your own thumbnails of sites in an images folder and your own descriptions in MySQL fields. You'll need the PrintScreen key for screen capture and a graphics program like ImageForge or at least Irfanview for the thumbnails data.
This page will give you the code needed to delete a URL from a website directory. In cms-creating-website-directories.html we saw how to create the website directory, and URLs were added one URL at a time. We'll do that here as well with deletion.
We assume you have access to a MySQL database and permission and password and user name, and you know the db name. We assume you know what is in a config file, since the PHP on this page uses include_once"config.php". If this is confusing, see the-configure-file.html. If you wish to see our directory demo, which shows how the directory looks and works, go to cms-view-website-directory-demo.html.
So let's peruse the code. There is a db table called urls—which has the fields url and category and id. In deleting, the URL and its category and id fields all add up to one row or record, and that is what will get deleted.
Next comes the input filters. URL names must use acceptable URL characters—our preg_replace() sees to that. Notice that for security, we used strip_tags() on the URLs as well. The strip_tags() is run before the replace so that the acceptable characters between tags, like the B between < and >, are dumped as well as the tags. If these latter were dumped in the preg_replace(), the in-the-tags debris would remain.
Before deleting, the urls table is searched for the one entered in the form. If it does not exist, a message reveals to the user that it does not exist. If it's in the db, the URL gets deleted from the urls table.
Finally, the HTML code gives users the form for entering URLs as well as instructions for URL entry.
SAVE THIS PAGE AS: cms-edit-website-directory-delete-url.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 Website Directories—Content Management System (CMS)—Delete URL</TITLE>
<meta name="description" content="Editing Website Directories—Content Management System (CMS)—Delete URL">
<meta name="keywords" content="Delete URL,Editing Website Directories,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;}
.url {position:absolute;top:0px;left:10px;width:989px}
.urls {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\\.\\,\\!\\;\\-\\_\\*\\@\\=\\+\\$\\/\\&\\[\\]\\#\\?\\047\\:\\(\\)]/i';
$replacement = '';
$U=$_POST['url'];
$U=strip_tags($U);
$U=preg_replace($pattern2, $replacement, $U);
if (strlen($U)>3){
$check_user_data = mysql_query("SELECT url FROM urls WHERE url='$U'") or die(mysql_error());
if(mysql_num_rows($check_user_data) == 0)
{echo '<script language="javascript">alert("This url is not in your DB. Please try again.")</script>;';
}else{
mysql_query("DELETE FROM urls WHERE url='$U'") 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-website-directory-delete-url.php";</script>';}
else{echo '<script language="javascript">alert("The deleting was unsuccessful.");</script>';}
}}
unset($U);
mysql_close();
?>
<div id='top' class='url'>
<h1>Editing Website Directories—Content Management System (CMS)—Delete URL</h1>
<div id='info' class='info'><h3>To delete a URL, copy it from the address bar of the site in question and put it into the input box, or simply type it in.</h3></div>
</div>
<div class='urls'>
<form method="post" action="cms-edit-website-directory-delete-url.php">
<table width="700" border="0" cellpadding="2" cellspacing="2" align="center">
<tr>
<td width="130">URL</td>
<td width='570'><input name="url" type="text" size="100"></td>
</tr>
<tr>
<td> </td><td><input name="retrieve" type="submit" value="Delete URL in DB"> <br><br>
<input name="reset" type="reset" value="Reset"></td>
</tr>
</table>
</form>
</div>
<?php include("nav.html"); ?>
</body>
</html>