Ok now we are going to learn how to make a CMS in php and mysql ...
**if you have any problems you can email me ...
martian2k4@yahoo.co.uk **
1. Open up Notepad and enter this code. This is a simple html form.
<form name="form1" method="post" action="submit.php">
Message Title :.
<input name="title" type="text" id="title"><br><br>
Message :.
<textarea name="message" id="message"></textarea><br><br>
<input name="Submit" type="submit" id="Submit" value="Submit message">
</form>
Save that as submit.html
2. Now time for the mysql. Run this query
CREATE TABLE `cms` (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(255) NOT NULL,
`message` VARCHAR(255) NOT NULL
);
this has created the table cms with 4 fields one for the id one for the title,
message and date.
3. Now time for the first php page. Save this as submit.php
<?php
$user = "your username";
$pass = "your password";
$host = "normally this would be localhost";
$db = "this is where the database name goes";
mysql_connect($host,$user,$pass) or die("Can not connect to mysql")
mysql_select_db($db) or die("Can not select the database")
$title = $_POST["title"];
$message = $_POST["message"];
$sql = "INSERT INTO cms (id, title, message) VALUES ('NULL', '$title','$message')";
$query = mysql_query($sql) or die("Can not connect to the MySql database.");
echo "Database updated :-).";
?>
Now lets go through it.
$user = "username";
$pass = "password";
$host = "host";
$db = "database";
mysql_connect($host,$user,$pass) or die("Can not connect to mysql")
mysql_select_db($db) or die("Can not select the database")
this is connecting to the mysql database you need to change username to
the username of the database the password to the password of it. host is normally
localhost and database is what ever the database is called.
$title = $_POST["title"];
$message = $_POST["message"];
$sql = "INSERT INTO cms (id, title, message) VALUES ('NULL', '$title','$message')";
$query = mysql_query($sql) or die("Can not connect to the MySql database.");
echo "Database updated :-).";
this is inserting the data into the database and then echoing a message saying
'Database updated :-).'.
4. the next php page, call this view.php
<?php
$user = "your username";
$pass = "your password";
$host = "normally this would be localhost";
$db = "this is where the database name goes";
mysql_connect($host,$user,$pass) or die("Can not connect to mysql")
mysql_select_db($db) or die("Can not select the database")
$retrive = mysql_query("SELECT * FROM cms ORDER BY id DESC");
while($r=mysql_fetch_array($retrive)){
extract($r);
echo $title;
echo '<br>';
echo $message;
}
?>
Now lets go though it.
$user = "your username";
$pass = "your password";
$host = "normally this would be localhost";
$db = "this is where the database name goes";
mysql_connect($host,$user,$pass) or die("Can not connect to mysql")
mysql_select_db($db) or die("Can not select the database")
Connecting to the MySql database.
$retrive = mysql_query("SELECT * FROM cms ORDER BY id DESC");
while($r=mysql_fetch_array($retrive)){
extract($r);
echo $title;
echo '<br>';
echo $message;
}
?>
This query's the database and then display's the info.
Thats then end of the tutorial but soon another tutorial written by me will be along
and we will make another cms but more advance with a control panel. So keep
checking back on
http://www.devdreams.com and
http://www.ur-fragged.co.uk