Hi All,
I have created a database where a user can add, delete and edit news articles. The use of the checkbox is to make the article active (to appear on site) or have it unactive.
The below code is for the edit page. The only problem is that if in the edit page the user changes the tickbox to ticked or vice versa i need it to then reflect that onto the database (ie. if checkbox changed to ticked, value = TRUE)
Any help would be appreciated:
$news_date=($_POST['news_date']);
$news_summary=($_POST['news_summary']);
$news_story=($_POST['news_story']);
$active=($_POST['active']);
$id = ($_GET['modify_id']);
if (($POST['submitted']) && (!$GET['modify_id'])) {
// the user has submitted a new listing
//write to database
mysql_query ("INSERT into news (news_date, news_summary, news_story, active) VALUES ('$news_date', '$news_summary', '$news_story', '$active')", $connectID)
or die ("Unable to insert record into database");
if ($success) {
header ("Location: results.php");
}
} elseif ((!$POST['submitted']) && ($GET['modify_id'])) {
// the admin page has passed ID of record to be updated
$this_Record = mysql_query("SELECT id,news_date,news_summary,news_story,active FROM news WHERE id='$id'", $connectID)
or die ("Can't read the this record.");
$record_data = mysql_fetch_array($this_Record, MYSQL_ASSOC);
} elseif (($POST['submitted']) && ($GET['modify_id'])) {
// the user has submitted the form to update a listing
$success = mysql_query("UPDATE news SET news_date = '$news_date', news_summary = '$news_summary', news_story='$news_story', active='$active' WHERE id='$id'", $connectID);
if ($success) {
header ("Location: results.php");
}
} else {
// The user has loaded the page to enter a new listing
// do nothing - just let the page load
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Edit News</title>
<style type="text/css">
body {font-family:verdana, arial, sans-serif; font-size:80%}
h3 {padding:10px 0 0 0; margin:0;}
label {display:block; margin:8px 0 2px 0;}
a {display:block; color:#066; margin:3px 0 10px;}
a:hover {color:#000; text-decoration:none;}
input[type="submit"] {display:block; margin-top:8px;}
</style>
</head>
<body>
<h3>Edit a listing</h3>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<!--news_date-->
<label for="news_date">News Date (d/m/yy)</label>
<input name="news_date" type="text" size="10" id="news_date" value="<?php print $record_data['news_date'] ?>" /></label>
<!--news_summary-->
<label for="news_summary">News Summary - to appear on homepage</label>
<input name="news_summary" type="text" size="60" id="news_summary" value="<?php print $record_data['news_summary'] ?>" />
<!--topic_description-->
<label for="news_story">News Story</label>
<textarea name="news_story"><?php print $record_data['news_story'] ?></textarea>
<br />
<?php
if($record_data['active'] == 'T')
{$checked = "checked";}
else
{$checked = "";}
?>
<?php print '<input type="checkbox" name="active" value="'.$record_data['id'].'checked="'.$checked.'" />'; ?>
<input type="submit" value="Submit" name="submitted" />
</form>
</form>
<p><a href="/add_news.php">Add New News</a><br />
<a href="/results.php">Back to News Listings</a></p>
</body>
</html>
<?php
// close the connection
mysql_close($connectID);
?>