Here is the script in action. If you hit "Edit News" it brings up some more links. When you click one of these links, it should display a form field with the values filled in (the values are coming from a database), but it doesn't. Any idea as to why? Here is the code:
<html>
<head>
<title></title>
</head>
<body>
<?php
$db = mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("dbname", $db) or die(mysql_error());
if(empty($_GET['action']))
{
echo("<a href='test4.php?action=submitnews'>Submit News</a><br />");
echo("<a href='test4.php?action=editnews'>Edit News</a><br />");
}
if($_GET['action'] == "submitnews")
{
echo("<form name='submitNews' method='post' action='$PHP_SELF'>");
echo("Subject: <input type='text' name='subject'><br />");
echo("News: <textarea name='subnews' rows='10' cols='45'></textarea><br />");
echo("<input type='submit' name='submit' value='Submit'><br />");
echo("</form>");
if($_POST['submit'] == "Submit")
{
$insert = mysql_query("INSERT INTO news (ID, date, subject, news) VALUES ('', NOW(), '$subject', '$subnews')") or die(mysql_error());
if($insert)
{
echo("Submitted.");
} else {
echo("Error.");
}
}
}
if($_GET['action'] == "editnews")
{
$getNews = mysql_query("SELECT * FROM news") or die(mysql_error());
while($row = mysql_fetch_array($getNews))
{
$id = $row['ID'];
$subject = $row['subject'];
$news = $row['news'];
echo("<a href='test4.php?action=editnews&newsid=$id'>Edit News ID# $id<br />");
}
echo($id);
if(($_GET['action='] == "editnews") && ($_GET['newsid'] == $id))
{
echo("<form name='newForm' method='post' action='$PHP_SELF'>");
echo("Subject: <input type='text' name='newsubject' value='$subject'><br />");
echo("News: <textarea name='newNewsBox' value='$news' rows='10' cols='45'></textarea><br />");
echo("<input type='submit' name='newSubmit' value='Submit'><br />");
echo("</form>");
if($_POST['newSubmit'] == "Submit");
{
$update = mysql_query("UPDATE news SET subject = '$newsubject', news = '$newNewsBox' WHERE ID = '$id'") or die(mysql_error());
if($update)
{
echo("Done.");
} else {
echo("Error updating.");
}
}
}
}
?>
Thanks. 🙂