Hi Russell,
Obviously, there will be better, more 'general' methods, but you can 'hack' a solution ...
<?php
#****************************
# ADD NEWS ENTRY
#****************************
//Check to make sure nobody attempts to access file directly
if(!defined('bcwgroup_news')){
echo 'You do not have permission to access this file';
exit;
}
$author = (isset($_POST['author'])) ? addslashes2($_POST['author']) : '';
$thedate = (isset($_POST['thedate'])) ? addslashes2($_POST['thedate']) : '';
$title = (isset($_POST['title'])) ? addslashes2($_POST['title']) : '';
$intro = (isset($_POST['intro'])) ? addslashes2($_POST['intro']) : '';
$body = (isset($_POST['body'])) ? addslashes2($_POST['body']) : '';
// Going to use this to decide whether or not to display the form
$news_was_added = false;
//ADD NEWS ENTRY
if($mode != '' && $action === 'add_news'){
//Did they fill in everything?
if($author != '' && $title != '' && $body != '' && $thedate != '' && $intro != ''){
//YES
$query = "INSERT INTO bcwgroup_news (author, thedate, title, intro, body, timestamp)
VALUES('" . $author . "', '" . $thedate . "', '" . $title . "', '" . $intro . "', '" . $body . "', '" . time() . "')";
if(mysql_query($query)){
//echo '<p style="text-align:middle;">News Added!</p>';
$news_was_added = true;
}else{
echo 'Problem Executing Query' . mysql_error();
}
}else{
//NO
echo 'Please fill in all fields!';
}
//RETRIEVE INFO FOR EDIT NEWS ENTRY
}elseif($mode === 'edit'){
if($action == ''){
$query = 'SELECT id, author, title , body, intro, thedate
FROM bcwgroup_news
WHERE id=' . $id . '';
$result = mysql_query($query);
$r = mysql_fetch_row($result);
$id = $r[0];
$author = stripslashes($r[1]);
$title = stripslashes($r[2]);
$body = stripslashes($r[3]);
$intro = stripslashes($r[4]);
$thedate = stripslashes($r[5]);
//Swap special symbols
$title = str_replace("%25", "%", $title);
$body = str_replace("%25", "%", $body);
$intro = str_replace("%25", "%", $intro);
$thedate = str_replace("%25", "%", $thedate);
$body = str_replace("%26", "&", $body);
$title = str_replace("%26", "&", $title);
$intro = str_replace("%26", "&", $intro);
$thedate = str_replace("%26", "&", $thedate);
}else{
//DO THE EDIT AND SEND INFO TO THE DATABASE
if($action === 'edit_news'){
$query = 'UPDATE bcwgroup_news
SET author="' . $author . '", title="' . $title . '", body="' . $body . '", intro="' . $intro . '", thedate="' . $thedate . '"
WHERE id =" ' . $id . '"';
if(mysql_query($query)){
echo 'Update Sucessful!';
}else{
echo 'Could not update News..' . mysql_error();
}
}
}
}
if($news_was_added){
// DON'T display the form
echo '<p style="text-align:middle;">News Added!</p> ... <a href="#">Return to wherever</a>';
} else {
// DO display the form
?>
<div style="margin-top: 0px;">
<div align="center">
<table style="margin: 0 auto;width:500px;top:1px;border:1px dotted #E5E5E5;" cellspacing="1">
<tr class="dark">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<td><strong>Author:</strong></td>
<td><input type="text" name="author" value="<?php echo $author; ?>" style="width: 120px;" /></td>
</tr>
<td><strong>Date:</strong></td>
<td><input type="text" class="fade" name="thedate" value="<?php echo $thedate; ?>" style="width: 120px;" /></td>
</tr>
<tr class="light">
<td><strong>Title:</strong></td>
<td><input name="title" type="text" style="width: 400px;" value="<?php echo $title; ?>" size="200"></td>
</tr>
<td><strong>Intro:</strong></td>
<td><textarea name="intro" cols="50" rows="5"><?php echo stripslashes($intro); ?></textarea></td>
</tr>
<tr class="dark">
<td><strong>Body:</strong></td>
<td><textarea name="body" cols="50" rows="10"><?php echo stripslashes($body); ?></textarea></td>
</tr>
<tr class="light">
<td colspan="2">
<div align="center">
<input type="hidden" value="<?php echo $mode; ?>" name="mode" />
<input type="hidden" value="<?php echo $id; ?>" name="id" />
<input type="hidden" value="<?php echo ($mode == 'add' ? 'add_news' : 'edit_news'); ?>" name="action" />
<input type="submit" value="Submit" name="submit" class="button" />
</div></td>
</tr>
</form>
</table>
<div id="instructions">
<p> * Dates should be entered in full, eg. 4th May 2005, 8th January 2005, 23rd November 2004</p>
</div>
</div>
</div>
<?php
}
?>
Have a look through what I've done ... see if you get the gist.
Paul 🙂