I've always set high bars for myself, so of course for my first REAL php project (besides forms and the like) I chose an entire CMS system. Oh well. Learning experiance, eh?
I can now add articles, sections, and testimonials. I've created multiple databases and a login/user system.
So what has caught me up? Deleting articles. Here is my code.
Code to make articles:
Code: Select all
<?php
// Get the PHP file containing the DbConnector class
require_once('includes/DbConnector.php');
require_once('includes/Validator.php');
// Create an instance of DbConnector
$connector = new DbConnector();
$validator = new Validator();
$result = $connector->query('SELECT ID FROM cmsarticles');
// Check whether a form has been submitted. If so, carry on
if ($HTTP_POST_VARS){
// Create an instance of DbConnector
$connector = new DbConnector();
// Validate the entries
$validator = new Validator();
$validator->validateTextOnly($HTTP_POST_VARS['title'],'Article Title');
$validator->validateGeneral($HTTP_POST_VARS['date'],'Article Date');
$validator->validateTextOnly($HTTP_POST_VARS['tagline'],'Tagline');
$validator->validateNumber($HTTP_POST_VARS['section'],'Section');
$validator->validateGeneral($HTTP_POST_VARS['thearticle'],'Article');
// Check whether the validator found any problems
if ( $validator->foundErrors() ){
echo 'There was a problem with: <br /><span style="color: red;">'.$validator->listErrors('</span> - Please be sure you are using only allowed characters <br>'); // Show the errors, with a line between each
}else{
// Create an SQL query (MySQL version)
// The 'addslashes' command is used 5 lines below for added security
// Remember to use 'stripslashes' later to remove them (they are inserted in front of any
// special characters
$insertQuery = "INSERT INTO cmsarticles (title,date,tagline,section,thearticle) VALUES (".
"'".$HTTP_POST_VARS['title']."', ".
"'".$HTTP_POST_VARS['date']."', ".
"'".$HTTP_POST_VARS['tagline']."', ".
$HTTP_POST_VARS['section'].", ".
"'".addslashes($HTTP_POST_VARS['thearticle'])."')";
// Save the form data into the database
if ($result = $connector->query($insertQuery)){
// It worked, give confirmation
echo '<center><b>Article added to the database</b></center><br>';
}else{
exit('Sorry, there was an error saving to the database');
}
}
}
?>This is right above the form to make articles, and it is withing the body tags.
Code to display article info and delete it
Code: Select all
<?php
// Require the database class
require_once('includes/DbConnector.php');
require_once('includes/Validator.php');
// Create an object (instance) of the DbConnector
$connector = new DbConnector();
$validator = new Validator();
// Execute the query to retrieve articles
$result = $connector->query('SELECT ID,title,date,tagline,thearticle FROM cmsarticles ORDER BY ID DESC LIMIT 0,5');
// DELETE SECTIONS ////////////////////////////////////////////////////////////////////
if ($HTTP_GET_VARS['action'] == 'delete'){
// Store the section ID to be deleted in a variable
$articleID = $row['ID'];
// Validate the sectionID, and if it's ok delete the section
if ( $validator->validateNumber($articleID,'Article ID') ){
// The validator returned true, so go ahead and delete the section
$connector->query('DELETE FROM cmsarticles WHERE ID = '.$articleID);
echo 'Article Deleted.<br>';
}else{
// The validator returned false, meaning there was a problem
echo "Couldn't delete. There was a problem with: ".$validator->listErrors();
}
}
// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){
echo '<div style="width: 500px;">
<span style="width: 50%; float: left;">';
echo '<b>Title: </b>';
echo stripslashes($row['title']);
echo '</span><span style="width: 50%; float: right;">';
echo '<b>Date Added: </b>';
echo $row['date'];
echo '</span><div style="width: 100%;">';
echo '<b>Article: </b>';
echo stripslashes(substr($row['thearticle'], 0, 300));
echo '</div><ul style="width: 100%; padding: 0px; "><li style="display: inline; padding: 0px; margin-right: 150px; text-align: center;">';
echo 'Edit';
echo '</li><li style="width: 50%;display: inline; margin-right: 150px; ">';
echo '<a href="articles.php?action=delete&id='.$row['ID'].'"> Delete </a>';
echo '</li><li style="width: 50%; display: inline">';
echo '<a href="http://www.noparentleftbehind.net/viewArticle.php?id='.$row['ID'].'">';
echo 'more';
echo '</a></li>';
echo '</ul></div>';
echo '<hr />';
}
?>It gives me an error for 'Article ID'
How can I get this to work?
Keep in mind, I'm a noob.