(Untested) First lets create a Mysql database,table. If you have php enabled and a mysql database capability the below script can be ran in phpmyadmin or see the link below to create a database.
upload this file and call the page
createdatabasenews.php
<?php
//Create database news
$host = "yourhost";
$username = "yourusername";
$password = "yourpassword";
$link = mysql_connect($host,$username,$password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$sql = 'CREATE DATABASE news';
if (mysql_query($sql, $link)) {
echo "Database news created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
?>
Next you need tables within that database, upload this file and call the page,
createnewsstory.php
<?PHP
//Create table news story
$host = "yourhost";
$username = "yourusername";
$password = "yourpassword";
$database = "news";
mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE news story (id int(6) NOT NULL auto_increment,headline VARCHAR(60) NOT NULL,story text NOT NULL,photo_id VARCHAR(60) NOT NULL,date date NOT NULL default '0000-00-00',PRIMARY KEY (id),UNIQUE id (id)";
mysql_query($query);
if (mysql_query($query) {
echo "Table news story created successfully\n";
} else {
echo 'Error creating table news: ' . mysql_error() . "\n";
}
?>
We now have the structure of our database to which we can add to if we wish.
For the photo ID you will need to create a folder on your server named uploaded_files or something similar, you will see the upload link later.
Now lets think of a form with some basic php code to insert that data into the database.
newsupload.php
<style type="text/css">
.list_left{
background-color:#4486BB;
font-weight:bold;
font-size:11px;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
.list_right{
background-color:#C6ECFF;
color:#333333;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
.NewClass { }
.NewClass { }
</style>
</head>
<?PHP
if($_POST['submit']=="send") {
$errcount = 0;
//can set errors with css
$error1="<font face='Verdana' size='1'><span style='color:red'>";
$error2="<font face='Verdana' size='1'><span style='color:red'>";
$error3="<font face='Verdana' size='1'><span style='color:red'>";
$error4="<font face='Verdana' size='1'><span style='color:red'>";
$error5="<font face='Verdana' size='1'><span style='color:red'>";
//check if someone has filled out a form
//validate headline
if(preg_match('/^[a-zA-Z0-9_ -]{2,}$/i', $_POST['headline'])){
$headline = $_POST['headline'];
}
else {
$error1 .= " Headline Required!";
$errcount++;
}
if(empty($_POST['story'])){
$error2 .= " News Story Required!";
$errcount++;
}
else {
$story = $_POST['story'];
}
$now =time();
// set a max file size for the html upload form
$max_file_size = 1048576; // size in bytes
$uploaddir = "uploaded_files/";
$valid_types = array("image/jpeg","image/jpg","image/gif","image/png");
$filetype = $_FILES['File']['type'];
$photo_id = $now.'-'.$_FILES['File']['name'];
$tmpname = $_FILES['File']['tmp_name'];
$size = $_FILES['File']['size'];
if (is_uploaded_file($_FILES['File']['tmp_name']))
{
//Get the Size of the File
$size = $_FILES['File']['size'];
//Make sure that $size is less than $size_byte
if ($size > $max_file_size)
{
$error3 .= "File Too Large!.";
$errcount++;
}
//check file type
if (in_array($filetype,$valid_types)) {
}
else
{
$error4 .= " Valid Image Types Only!";
@unlink($_FILES['File']['tmp_name']);
$errcount++;
}
if ($errcount !=0) {
//displays errors in form boxes
}
else {
move_uploaded_file($tmpname, $uploaddir . $photo_id);
// databse connection info
$host = "yourhost";
$username = "yourusername";
$password = "yourpassword";
$database = "News";
$story = htmlspecialchars($story);
$story = nl2br($story);
$story = trim($_POST['story']);
$story = wordwrap($story, 65);
//make the connection and show errors if you cant connect
$conn = mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($database,$conn) or die(mysql_error());
//Insert into our table or everyrthing from table
$query = "INSERT INTO news story VALUES('','$headline','$story','$photo_id',NOW())";
mysql_query($query);
$headline = mysql_real_escape_string($_POST['headline']);
$story = mysql_real_escape_string($_POST['story']);
$photo_id = mysql_real_escape_string($_POST['photo_id']);
//redirect if the code runs OK to newsheadlines.php
echo '<meta http-equiv="refresh" content="0;url=http://www.oursite.co.uk/newsheadlines.php">';
}
}
}
?>
<form id="Upload" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table cellpadding="3" cellspacing="1">
<tr>
<td class="list_left">Headline:</td>
<td class="list_right"><input type="text" value="<? echo $headline; ?>" id="headline" name="headline" style="width:200px;" ><?php echo $error1; ?></td>
</tr>
<tr>
<td class="list_left">Story:*<br></td>
<td class="list_right"><textarea name="story" id="story" rows="5" cols="45"><?php echo $story; ?></textarea></td>
</tr>
<tr>
<td class="list_left">Photo Upload:*</td>
<td class="list_right"><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
<input type="file" name="File" size="20" value="<?php echo $photo_id; ?>"><?php echo $error3; ?><?php echo $error4; ?></td>
</tr>
<tr>
<td colspan="2" class="list_left" align="center"><input id="submit" name="submit" type="submit" value="send" /> <input type='button' value='Retry' onClick='history.go(-1)'> <input type="button" value="Reset Location" onclick="resetListGroup('chainedmenu')"></td>
</tr>
<tr>
<td colspan="2" class="list_left" height="25" align="center"><?php echo $error2; ?> <?php echo $error3; ?> <?php echo $error4; ?></td>
</tr>
</table>
</form>
</body>
</html>
newsheadlines.php
<!--CSS HERE ETC!>
<?PHP session_start();
// databse connection info
$host = "yourhost";
$username = "yourusername";
$password = "yourpassword";
$database = "news";
//make the connection and show errors if you cant connect
$conn = mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($database,$conn) or die(mysql_error());
//Insert into our table or everyrthing from table
$query = "SELECT * FROM news story ORDER by date DESC";
mysql_query($query);
$result=mysql_query($query);
$num=mysql_num_rows($result);
if ($num==0) {
$empty = "No News Storys Today";
}
else {
$num_rows = mysql_num_rows($result);
}
$i=0;
//create a while loop of all records on from our result
while ($row = mysql_fetch_array($result)) {
$id = $row["id"];
$headline = $row["headline"];
$photo_id = $row["photo_id"];
$date = $row["date"];
$story = nl2br($story);
//if no image is available use a default image
if(empty($photo_id))
{
$photo_id = 'default_image.JPG';
}
echo "<table cellpadding='0' cellspacing='0' border='0' align='cente' width='800px'>";
echo "<tr><td valign='top' align='center' width='800px'>";
echo "<table cellpadding='0' width='800px' cellspacing='3'>";
echo "<tr><td align='left'>";
echo "<table border='0' cellpadding='0' cellspacing='0' width='800px' class='title3'>";
echo "<tr><td width='81%' style='line-height:19px;'> $headline</td></tr>";
echo "<td width='19%'>$date</td>";
echo "</tr>";
echo "</table>";
echo "<table border='0' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td valign='top'><a href ='./uploaded_files/$photo_id' /'><img alt='$headline on $date' title='Click and View Image to see full size' border='0' style='border-color:#999999' src='./uploaded_files/$photo_id' /></a></td>";
echo "</table><br>";
echo "<table border='0' cellpadding='2' cellspacing='0' width='600px' class='box'>";
echo "<td align='center' class='body' cellpadding='2'><text name='story' id='story' rows='15' cols='45'>$story</td>";
echo "</tr>";
echo "</table>";
echo "</table>";
echo "</td></tr>";
echo "</table>";
?>
</form>
</tr>
<?
$i++;
}
?>
<?PHP echo $empty; ?>
Hope this helps!