I have a simple website where people can add/edit/delete announcements. For adding and editing I send them to a page (news.php) and based on their choice the correct information is displayed - a blank form for adding or the form with the pre-populated info of the item they are editing. That form then gets processed through another script (commitnews.php) and should either add or update the item accordingly then send the user back to the news page. However, whenever I submit the info, it takes me to commitnews.php page but it is blank and info is not changed in the db.
The URL looks right:
http://localhost/mysite/commitnews.php?cmd=edit&news_id=9
Link from news page:
echo" <a href='news.php?&cmd=add&news_id='>ADD</a></h3>";
echo "<a href='news.php?&cmd=edit&news_id=$news_id'>EDIT</a>";
Code on news.php
include 'data.php';
switch ($_GET['cmd']) {
case "edit":
$newssql = "SELECT * FROM news
WHERE news_id = '" . $_GET['news_id'] . "'";
$result = mysql_query($newssql) or die("Invalid query: " . mysql_error());
$row = mysql_fetch_array($result);
$title = $row['title'];
$body = $row['body'];
break;
default:
$title = "";
$body = "";
break;
}
<form action="commitnews.php?action=<?php echo $_GET['cmd']; ?>&news_id=<?php echo $_GET['news_id']; ?>" method="post">
News Title: <input type="text" name="title" value="<?php echo $title; ?>"><br />
News Body: <textarea name="body" cols="50" rows="20"><?php echo $body; ?></textarea>
<input type="submit" name="submit" value="<?php echo $_GET['cmd']; ?>">
</form>
Code for commitnews.php
include 'data.php';
switch ($_GET['cmd']) {
case "edit":
$sql = "UPDATE news SET
title = '" . $_POST['title'] . "',
body = '" . $_POST['body'] . "',
WHERE news_id = '" . $_GET['news_id'] . "'";
break;
case "add":
$sql = "INSERT INTO news
(title,
body)
VALUES
('" . $_POST['title'] . "',
'" . $_POST['body'] . "')";
break;
}
$result = mysql_query($sql)
or die("Invalid query: " . mysql_error());
header("Location: index.php");