Dynamic website part 4 – Deleting records

by krike in PHP & MYSQL / Tutorials on 07 Jan 2012


In the previous tutorials you learned how to read from the database and how to add new records. This time we will learn how to delete specific records.

First of all we will quickly add some links in the sidebar so we can navigate throught the pages more quickly.

<ul>
<li><a href="index.php">Homepage</a></li>
<li><a href="articles.php">Articles</a></li>
<li><a href="addarticle.php">Add article</a></li>
</ul>

Now open the articles.php file. Under your article (within the while loop) add a delete link:

<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=delete&article_id=<?php echo  $row['id']; ?>">Delete</a>

Let’s go to the top of the page (but under the header include line) and catch our parameters. First we will see if there is a query parameter ‘action’ and if it is the delete action. We also prepare our trycatchto check if an error occured.

if(isset($_GET['action']) && $_GET['action'] == 'delete')
{
	try{
		//check if an id was given and if it's a number
		//delete record
	}
	catch(Exception $e){
		echo '<div class="feedback error">' . $e->getMessage() . '</div>';
	}
}

We first check for the id and make sure it is a number, if not we throw an exception. (place this in the trycatch)

//if it does not exist
if(!isset($_GET['article_id']))
{
//throw an exception
	throw new exception('No id given');
}
//if it's a number
if(is_numeric($_GET['article_id'])){
	//store it in a variable
	$id = mysqli_real_escape_string($link, $_GET['article_id']);
} else {
	//else throw an exception
	throw new exception('The given id is not valid, only numbers!');
}

Now that we have the id of the article to delete we can create our query.

if(!mysqli_query($link, 'DELETE FROM article WHERE id = ' . $id)){
	//if there was a problem executing the query throw an exception
	throw new exception('Could not delete record from the database');
} else {
	//display a success message
	echo '<div class="feedback success">Query successfully deleted.</div>';
}

When you click the delete action you should now see a success message.

You can then add some styling for the error/success message

/*ERROR MESSAGE*/
.error
{
padding: 8px 0px 8px 40px;
margin:5px 0px;
-moz-border-radius:4px;
border:1px solid #BF0008;
background:#FFDFDE url("../images/single_images/delete.png") no-repeat;
background-position:10px 10px;
color: #BF0008;
}

.error span
{
	font-weight:bold;
}

.error a:link, .error a:visited
{
color:#BF0008;
font-weight:bold;
text-decoration:underline;
}

.error a:hover, .error a:active
{
color:#FFF;
background:#BF0008;
font-weight:bold;
text-decoration:underline;
}

/*SUCCES MESSAGE*/
.succes
{
padding: 8px 0px 8px 40px;
margin:5px 0px;
-moz-border-radius:4px;
border:1px solid #057100;
background:#DFFFDE url("../images/single_images/check.png") no-repeat;
background-position:10px 10px;
color: #057100;
}

.succes span
{
	font-weight:bold;
}

.succes a:link, .succes a:visited
{
color:#057100;
font-weight:bold;
text-decoration:underline;
}

.succes a:hover, .succes a:active
{
color:#FFF;
background:#057100;
font-weight:bold;
text-decoration:underline;
}

Written by krike

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet nisl nisl. Ut interdum libero vitae quam ultricies et lacinia elit aliquet. Praesent tincidunt, sem tempus feugiat feugiat, turpis tellus scelerisque erat, sit amet feugiat neque arcu ac lectus. Sed at mi et elit interdum scelerisque vitae eu felis.

krike has written 77 posts.

  • shari

    Hello, just wanna say a big thank u for the great tutorials, and also i just wanna know when u gonna publish the rest parts of the tutorials?

    Thanks.

    • http://cmstutorials.org krike

      You are welcome. hopefully as soon as the blog is completed updated. I’m currently working on creating a new blog installation and moving all post, comments, … to it. Could take a few more days.