Welcome to my basic CMS tutorial. This CMS uses a MySQL database to store the articles and php files to add, display, and remove them. First off, I am going to explain the features. It has a list page, where it lists the articles in the database by their title as links that take you to the article you want. It has an edit page, that shows all of the articles with a button to delete. And last but not least, a page for adding new articles.
But before we get to that, let's get our MySQL database setup. Go into your phpMyadmin, make a new database named cms, execute this query on it (on the third line, where it says NAME - put your name.)
CREATE TABLE IF NOT EXISTS `articles` (
`title` varchar(100) NOT NULL,
`author` varchar(50) NOT NULL DEFAULT 'NAME',
`content` text NOT NULL,
`date` varchar(10) NOT NULL,
`id` mediumint(5) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
Now, let's make db.php, this will contain the MySQL connect code for all the other files to include and use. (Replace username and password with your MySQL user and pass.)
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cms", $con);
?>
Now let's make list.php. Paste this code, pretty simple stuff.
<?php
include 'db.php';
$result = mysql_query("SELECT * FROM articles ORDER BY id ASC");
while($row = mysql_fetch_assoc($result))
{
echo "<p>";
echo "<a href='display.php?id=".strip_tags($row['id'])."'>".strip_tags($row['title'])."</a>";
echo "</p>";
}
?>
What that does is connects to database, and spits out the titles of all the articles, as links to view them.
Now we need to make display.php, list.php links to it to view whole articles.
<?php
$id=$_GET['id'];
include 'db.php';
$result = mysql_query("SELECT * FROM articles WHERE id='$id'");
while($row = mysql_fetch_assoc($result))
{
echo "<p>";
echo "<strong>".strip_tags($row['title'])."</strong>";
echo "<br />";
echo "".strip_tags($row['content'])."";
echo "<br />";
echo "By: <strong>".strip_tags($row['author'])."";
echo "<br />";
echo "<strong>".strip_tags($row['date'])."";
echo "</p>";
}
?>
Now we need a page to add articles, right? add.php:
<?php
$date = date('m\-d\-Y');
include 'db.php';
if(isset($_POST['submit']))
{
$title = mysql_real_escape_string($_POST['title']);
$author = mysql_real_escape_string($_POST['author']);
$content = mysql_real_escape_string($_POST['content']);
$date = mysql_real_escape_string($_POST['date']);
if ($title == "")
{
echo "Please insert a title.";
exit();
}
if ($author == "")
{
echo "Please put an author name.";
exit();
}
if ($content == "")
{
echo "Please put content for your article.";
exit();
} if ($date == "")
{
echo "Date Fail.";
exit();
}
$query = "INSERT INTO articles (title,author,content,date)
VALUES ('$title','$author','$content','$date')";
$result = mysql_query($query)
or die ("Fail.");
echo "Win. Go to List All page to see your new article.";
exit();
}
else {
echo "<p><strong>Post a new article.</strong></p>";
echo "<p><div id='form'><form action='{$_SERVER['PHP_SELF']}' method='POST'><br /></p>
<p>Title:<br /><input type='text' name='title' /><br /></p>
<p>Author (That's you!)<br /><input type='text' name='author' /><br /></p>
<p>Content: <br /><textarea name='content' cols='60' rows='10'</textarea><br /></p>
<p>Date: <br /><input type='text' name='date' value='$date' /><br /></p></div>
<p><input type='submit' name='submit' /></p>
";
}
?>
Note: I HIGHLY suggest that you password protect this either with .HTACCESS or php.
Last thing, is a page that lists articles and allows you to delete them. (Password protect this too!) edit.php
<?php
include 'db.php';
$result = mysql_query("SELECT * FROM articles");
while($row = mysql_fetch_assoc($result))
{
echo "<p>";
echo "<form action='edit2.php' method='POST'>";
echo "<strong>".strip_tags($row['title'])."</strong>";
echo "<br />";
echo "".strip_tags($row['content'])."";
echo "<br />";
echo "By: <strong>".strip_tags($row['author'])."";
echo "<br />";
echo "<strong>".strip_tags($row['date'])."";
echo "<input type='hidden' name='id' value='".strip_tags($row['id'])."' />";
echo "<br /><input type='submit' name='submit' value='Delete this article' />";
echo "</form>";
echo "</p>";
}
?>
One more for the deleting feature. edit2.php
<?php
include 'db.php';
$id = $_POST['id'];
echo "$id";
$result = mysql_query("DELETE FROM articles WHERE id='$id'");
echo "Deleted!";
?>
Now, upload that all to your server. Now direct your browser to add.php and put a test article. After that go to list.php to see it.
That's it!

