puggles Posted March 27, 2008 Share Posted March 27, 2008 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. so now, it writes to database if checkbox active T or not. Only problem is, that each time i now go t the edit page, the tickbox is ticked regardless of being T or not. How can i include the code so that not only does it write to the database as T or not, but that if the database says it is T, the box is already ticked?? Any help would be appreciated: <?php $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 ($checked = 'checked') {$active = 'T';} else { $active = '';} ?> <?php print '<input type="checkbox" name="active" value="'.$active.'" "'.$checked.'" />'; ?> </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); ?> (edited by kenrbnsn to add tags) Quote Link to comment Share on other sites More sharing options...
schilly Posted March 27, 2008 Share Posted March 27, 2008 <?php if ($checked = 'checked') {$active = 'T';} else { $active = '';} ?> change to $checked == 'checked' Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.