i'm creating an editable news system for my site, which i had a spot of trouble with a few weeks ago - it wasn't loading the confirmation page. someone sorted it out for me on here (thanks) but since then it's gone a bit weird again.
when the 'delete' function is used, it deletes the last entry as opposed to the one selected...and the edit function just creates a new entry instead of editing the topic selected.
i've had a mate look at the code but he can't see what i've done wrong...it would be useful to be able to edit the news entries 'live' - but at the moment i'm tempted to leaving it as an updatable function with no arrangement for edits and deletes as it's beginning to bug me...
the code is below...thanks in advance for any help...like i said before, i'm only starting to get to grips with all this :rolleyes:
<html>
<head>
</head>
<body>
<?php
//edit news
if($_GET['action'] == "edit" && isset($_POST['password'])) {
if($_POST['password'] == "password") {
$line = $_POST['date'] . "|";
$line .= $_POST['news'];
$line = str_replace("\r\n","<br>",$line);
$line .= "\r\n";
$data = file('news.txt');
$data[$id] = $line;
reset($data);
$fp = fopen('news.txt','w');
foreach($data as $element) {
fwrite($fp, $element);
}
fclose($fp);
echo "news item edited<br>\n";
echo "<a href=\"$PHP_SELF\">go back</a>\n";
exit;
} else {
echo "<h3><b>error:</b></h3>\n
invalid password";
exit;
}
}
if($_GET['action'] == "edit") {
$data = file('news.txt');
$element = trim($data[$id]);
$pieces = explode("|", $element);
$news = str_replace("<br>","\r\n",$pieces[1]);
echo "make the changes and press save<br>\n";
echo "<form action=\"$PHP_SELF?action=edit\" method=\"post\" name=\"editform\">\n";
echo "the news:<br>\n";
echo "<textarea name=\"news\" cols=\"40\" rows=\"5\">".$news."</textarea><br><br>\n";
echo "password:<br>\n";
echo "<input type=\"password\" size=\"30\" name=\"password\"><br>\n";
echo "<input type=\"hidden\" name=\"date\" value=\"".$pieces[0]."\">\n";
echo "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
echo "<input type=\"submit\" name=\"submit\" value=\"save\"><br>\n";
echo "</form>\n";
exit;
}
//delete news
if($_GET['action'] == "delete" && isset($_POST['password'])) {
if($_POST['password'] == "password") {
$data = file('news.txt');
array_splice($data,$id,1);
$fp = fopen('news.txt','w');
foreach($data as $element) {
fwrite($fp, $element); }
fclose($fp);
echo "<b>item deleted</b><br><br>\n";
echo "<a href=\"$PHP_SELF\">go back</a>\n";
exit;
}
else {
echo "<h3><b>error:</b></h3>\n
invalid password";
exit; }
}
if($_GET['action'] == "delete") {
echo "<h3>you are about to delete this news item</h3>\n";
$data = file('news.txt');
$element = trim($data[$id]);
$pieces = explode("|", $element);
echo "<div class=\"date\">\n" . $pieces[0] . "\n</div>\n" . "<div class=\"news>\">\n" . $pieces[1] . "\n</div>\n<br>\n\n";
echo "<br><br>\n";
echo "please enter your password<br>\n";
echo "<form action=\"$PHP_SELF?action=delete\" method=\"post\" name=\"deleteform\">\n";
echo "password:<br>\n";
echo "<input type=\"password\" size=\"30\" name=\"password\"><br>\n";
echo "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
echo "<input type=\"submit\" name=\"submit\" value=\"delete\"<br>\n";
echo "</form>\n";
exit;
}
//current news
echo "<h3>current news</h3>\n";
$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $key=>$element) {
$element = trim($element);
$pieces = explode("|", $element);
echo "<div class=\"date\">\n" .
$pieces[0] . "\n</div>\n" .
"<div class=\"news>\">\n" .
$pieces[1] . "\n</div>\n<br>\n\n";
echo " <a href=\"$PHP_SELF?action=delete&id=$key\">delete</a>\n";
echo " <a href=\"$PHP_SELF?action=edit&id=$key\">edit</a>\n";
echo "<br><br>\n";
}
echo "<hr>\n";
echo "<h3>add news</h3>\n";
if($_POST['submit']) {
if($_POST['password'] == 'password') {
if(!$_POST['news']) {
echo "<h3><b>error:</b></h3>\n
you must enter some news";
exit;
}
if(strstr($_POST['name'],"|")) {
echo "<h3><b><error:/b></h3>
name cannot contain <b>|</b>";
exit;
}
if(strstr($_POST['news'],"|")) {
echo "<h3><b>error</b></h3>
news cannot contain <b>|</b>";
exit;
}
$fp = fopen('news.txt','a');
if(!$fp) {
echo "<h3><b>error:</b></h3>
cannot open file";
exit;
}
$line = date("d.m.y , H.i");
$line .= "|" . $_POST['news'];
$line = str_replace("\r\n","<br>",$line);
$line .= "\r\n";
fwrite($fp, $line);
if(!fclose($fp)) {
echo "<h3><b>error:</b></h3>
cannot close file";
exit;
}
echo "<h3><b>action successful:</b></h3>
your news has been submitted";
} else {
echo "<h3><b>error:</b></h3>
invalid password";
}
}
?>
<!--add news form-->
<form action="<?=$PHP_SELF?>" method="post" name="newsentry">
news:<br>
<textarea name="news" cols="40" rows="5"></textarea><br><br>
<input type="password" size="30" name="password"><br>
<input type="submit" name="submit" value="submit news"><br>
</form>
</body>
</html>