Let me explain what I have mentioned in my previous post.
This would be next stage of creating your project little bit in order.
This is also a basic concept but you should get an idea:
I will add 3 more files: functions.php, config.php, common.php
firstly config.php
PHP Code:
<?php
//db settings
define("BASESERVER","localhost");
define("BASEUSER","roots");
define("BASEPASS", "555456");
define("BASENAME","site");
?>
For now it's very bacis, it only holds database settings, but later you can add some global settings you need everywhere.
common.php
PHP Code:
<?php
include_once "config.php";
include_once "functions.php";
session_start();
//connect to the base
if(!($db = @mysql_pconnect(BASESERVER, BASEUSER, BASEPASS))) //connect to base host
die("Cannot connect to the base server.");
if(!@mysql_select_db(BASENAME,$db)) //select base
die("Database doesn't exist!!!");
//common functions
function my_query($sql)
{
GLOBAL $_SERVER["PHP_SELF"];
GLOBAL $db;
if(($result = @mysql_query($sql,$db)) == 0) //Execute SQL query
{
echo "\n<hr>Database error: <strong>".mysql_error()."</strong><br /><br />\n";
die("Query was (<strong>$sql</strong>) in file <strong>".$_SERVER["PHP_SELF"]."</strong>");
}
else
return $result;
}
?>
As you can see, common will include config and functions, start session and connect to database. Some people are against persistant connections to database, but I have never had problems with them in my life.
Also, you will notice my_query function which basically execute the given query. You will probably find id handy later on.
And functions.php
PHP Code:
/**
*
* Simple function for adding article
*
* Function expects an array with article details
* it will perform all neccesary checks
* On error it will set session with error and return false
* On success, it will add record into database and return true
*
*/
function article_add($data)
{
if(empty($data["article_name"]))
{
$_SESSION["error"] = "Please enter The Article Name";
return false;
}
else if(empty($data["summary"]))
{
$_SESSION["error"] = "Please enter a summary";
return false;
}
else if(empty($data["article_content"]))
{
$_SESSION["error"] = "Please enter a content";
return false;
}
$sql = "INSERT INTO articles SET";
$sql .= " article_name = '".$data["article_name"]."'";
$sql .= ", summary = '".$data["summary"]."'";
$sql .= ", author = '".$data["author"]."'";
$sql .= ", article_content = '".$data["article_content"]."'";
$sql .= ", notes = '".$data["notes"]."'";
$sql .= ", date_added = NOW()";
$sql .= ", Status = '0'";
my_query($sql);
return true;
}
For now, functions file have this simple function which will add article in database.
Now, your file which adds article in database should look like this:
PHP Code:
<?php
include_once "common.php";
$article["article_name"] = $_POST['articlename'];
$article["summary"] = $_POST['summary'];
$article["author"] = $_POST['author'];
$article["article_content"] = $_POST['bodycontent'];
$article["notes"] = $_POST['notes'];
if(article_add($article))
{
//success
//do whatever you want to do on succes
}
else
{
//error
$_SESSION["article"] = $article;
header("Location: add11.php");
}
?>
This way you will have to include common.php in every file, but it really makes it easier this way.
This is first stage of course, and lots of changes can be done to all those files, but it's wise to go step by step 
As for the sessions, they are good for usage for logging in users, but you should firstly decide where would you like to store username/password. I suggested htacces because it's the easiest way and it's just fine for the start.
Bookmarks