Deleting Authors from a PEAR Content Management System - Adding Authors
(Page 4 of 4 )
Finally, we take a look at the add_auth.php page that is responsible for adding authors to our CMS. Here’s the code that makes up this page:
<?php
session_start();
if($_SESSION['level']!=="admin"){
//redirect to login page
header("location:login.php");
}
session_start();
$err=false;
$error="";
//check if form is submitted
if(isset($_POST['key'])){
//make sure fields are not empty
if(empty($_POST['authname'])){
$err=true;
$error="Enter a author name<br>";
}
//make sure fields are string
if(is_numeric($_POST['authname'])){
$err=true;
$error .= "Invalid format";
}
if(!$err){
include 'db.php';
include 'connx.php';
$authname=mysql_real_escape_string($_POST['authname']);
$sql = "INSERT INTO authors(aid,name) VALUES (NULL,'".$authname."')";
$res=$db->query($sql);
if (DB::isError($res)) {
die($res->getMessage());
}
}else{
echo "The following errors occurred:<br> ".$error;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/main.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<link href="../Templates/pear.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="100%" border="0" class="bord">
<tr class="header1">
<td colspan="2"><div align="center">Content Management System </div></td>
</tr>
<tr>
<td width="5%" valign="top"><!-- InstanceBeginEditable name="EditRegion4" --><table width="100%" border="1">
<tr>
<td colspan="2" bgcolor="#0066FF"><strong>Section</strong></td>
</tr>
<tr>
<td class="nav">Logout </td>
<td><img src="../images/user.gif" width="45" height="55" alt="" /></td>
</tr>
<tr>
<td colspan="2" class="nav"><a href="index.php">Home</a></td>
</tr>
<tr>
<td colspan="2" class="nav">List Authors </td>
</tr>
<tr>
<td colspan="2" class="nav"><a href="authors.php">Add Authors</a></td>
</tr>
<tr>
<td colspan="2" class="nav"><a href="delete.php">Delete articles </a></td>
</tr>
</table><!-- InstanceEndEditable --></td>
<td width="95%" valign="top"><!-- InstanceBeginEditable name="EditRegion3" -->
<form id="form1" name="form1" method="post" action="">
<table width="100%" border="1">
<tr>
<td colspan="2" class="header1">Add New Author </td>
</tr>
<tr>
<td width="25%">Author name: </td>
<td width="75%"><label>
<input name="authname" type="text" id="authname" />
<input name="hiddenField" type="hidden" value="key" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="Submit" value="Add Author" />
</label></td>
</tr>
</table>
</form>
<!-- InstanceEndEditable --></td>
</tr>
<tr class="copy">
<td colspan="2">©2008</td>
</tr>
</table>
</body>
<!-- InstanceEnd --></html>
Here’s a screen of what the code produces:

There are two parts to this script. The first is the PHP code portion which is responsible for capturing and processing form data. The second part of the code is the HTML portion. It contains an HTML form that takes input from the user. We only require one piece of information from the user, which is the name of the new author that we want to add to the database.
Basically the user enters a name and then presses the submit button, at which point the PHP code will kick in and start processing the form data. In our next article we will look at the code in detail.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |