Lets get started
First for the SQL Code!
CREATE TABLE `newscms` ( `id` int(10) unsigned NOT NULL auto_increment, `date` varchar(50) default NULL, `title` varchar(50) NOT NULL default '', `message` text NOT NULL, `icon` varchar(100) NOT NULL default '', `user` varchar(50) NOT NULL default '', PRIMARY KEY (`id`), KEY `date` (`date`) );
This will set your tables up, hit the SQL button on your mysql program.
This is dbconnect.php
<? $username = "username"; //put your mysql username $password = "password"; //put your mysql password $host = "localhost"; //put your mysql host usually localhost $database = "database"; //put your mysql database name //Do not change these lines below mysql_connect($host,$username,$password) or die("Error connecting to Database! " . mysql_error()); mysql_select_db($database) or die("Cannot select database! " . mysql_error()); ?>
Now time for the display code, it is comment heavy so you can understand what it does.
<? include('dbconnect.php'); //connects to database //select the table $result = mysql_query("select * from newscms order by id desc limit 5"); //grab all the content from the table while($r=mysql_fetch_array($result)) { $id=$r["id"]; $title=$r["title"]; $date=$r["date"]; $user=$r["user"]; $icon=$r["icon"]; $message=$r["message"]; //displays the row's echo "<img src=' $icon' align='left'> <b>$title</b> Posted on $date <br>Posted by: <b> $user</b> <br>$message <br>"; } ?>
The display only shows the 5 newest news posts. Also save that as display.php
Time for the addnews.php
<? include('dbconnect.php'); ?> <form action="addnews.php" method="post"> <br>Title: <br><input name="title" type="text" value="Title"> <br>Author: <br><input name="user" type="text" value="Name"> <br>Date: <br><input name="date" type="text" value="<?php print date("F j Y"); ?>"> <br>Icon: <br><input name="icon" type="text" value="Icon URL"> <br>Message: <br><textarea name="message" cols="40" rows="6" value="Message"> </textarea> <br>Password: <br><input name="password" type="password"> <br><input name="submit" type="submit" value="Submit"> <?php $password="yourpassword"; //change this to the password you want if ($_POST['password']==$password){ //DO NOT CHANGE THIS LINE if (isset($_POST['submit'])) { include("dbconnect.php"); $title = addslashes(strip_tags($_POST['title'])); $user = addslashes(strip_tags($_POST['user'])); $icon = addslashes(strip_tags($_POST['icon'])); $message = $_POST['message']; $date = addslashes(strip_tags($_POST['date'])); $sql = "INSERT INTO newscms SET title='$title', user='$user', icon='$icon', message='$message', date='$date'"; if (mysql_query($sql)) { echo("Your news has been added."); } else { echo("Error adding entry: " . mysql_error() . ""); } } ?>
There is one thing you must change in this, its the password, change it to whatever you want.
Thats it, your done! Have fun! Modify it however you want! I have tested it and it works like a charm! Happy coding!
Thought everyone would like it
Edited by Donna, 28 September 2005 - 02:39 PM.