Lodius2000 Posted June 6, 2008 Share Posted June 6, 2008 ok so i am creating a blog cms, I have: a login page a view a list of entries in the database page, by title a entry creation page also I have integrated the database into my homepage but now i need an edit entry page i would like the title of each entry in the view a list of entries in the database page to be a link that links to an update page with the fields already filled out with the data in the database row. I would like to make 1 update page and then be able to fill the fields dynamically based on the entry title and the items in that database row that correspond with it the two fields required are a textarea and text field (as yet my cms is very basic) problem is I dont know how to make the link of the entry title go to the edit entry page I think it has something to do with $_GET but the 2 php books i have barely mention GET, a good tutorial would be appreciated, I cant find one if I am way off, some advice on how to get started would be appreciated thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/108923-solved-displaying-database-field-values-in-html-form-fields/ Share on other sites More sharing options...
DarkWater Posted June 6, 2008 Share Posted June 6, 2008 Make it a link to like, editpage.php?id=213. Then on that page, do: $id = $_GET['id']; //then do a check to make sure that the logged in user created this article //Then display it to the user: <input type="text" name="title" value="$row['title']" /> <textarea (put your cols and rows here)>$row['post']</textarea> Quote Link to comment https://forums.phpfreaks.com/topic/108923-solved-displaying-database-field-values-in-html-form-fields/#findComment-558813 Share on other sites More sharing options...
Lodius2000 Posted June 6, 2008 Author Share Posted June 6, 2008 Make it a link to like, editpage.php?id=213. Then on that page, do: so where does the id come from, is that the entry id, on that not how do i give the link that value <a href="editpage.php?<? $_GET ?>"> entry title here </a> kinda like that? Quote Link to comment https://forums.phpfreaks.com/topic/108923-solved-displaying-database-field-values-in-html-form-fields/#findComment-558817 Share on other sites More sharing options...
DarkWater Posted June 6, 2008 Share Posted June 6, 2008 When you display topics, have a link that you append the ID to. So yeah, sort of like what you did, but only: <a href="editpage.php?id=<?php $row['threadid'] ?>"> Assuming that's your mysql row. Quote Link to comment https://forums.phpfreaks.com/topic/108923-solved-displaying-database-field-values-in-html-form-fields/#findComment-558819 Share on other sites More sharing options...
Lodius2000 Posted June 6, 2008 Author Share Posted June 6, 2008 so i do $id = $_GET['id']; then i can do a SLQ query on the update page SELECT thread_title, thread_body FROM tablename WHERE threadid = $id right?, then i can put the variables returned into the forms and upon submit it UPDATEs instead of INSERTs ?? Quote Link to comment https://forums.phpfreaks.com/topic/108923-solved-displaying-database-field-values-in-html-form-fields/#findComment-558833 Share on other sites More sharing options...
DarkWater Posted June 6, 2008 Share Posted June 6, 2008 Pretty much, yeah. Quote Link to comment https://forums.phpfreaks.com/topic/108923-solved-displaying-database-field-values-in-html-form-fields/#findComment-558834 Share on other sites More sharing options...
Lodius2000 Posted June 6, 2008 Author Share Posted June 6, 2008 this sounds really easy, darkwater, helpful as always thank you will mark this as solved for now, until i actually try to do it heh Quote Link to comment https://forums.phpfreaks.com/topic/108923-solved-displaying-database-field-values-in-html-form-fields/#findComment-558838 Share on other sites More sharing options...
Lodius2000 Posted June 6, 2008 Author Share Posted June 6, 2008 so i created my edit page, modified my view a list of entries in the database page, so that each entry title in the list displays like this <?php foreach ($list as $row) { print '<tr><td>' . $row[article_id] .'</td><td><a href="../editform/index.php?id=' . $row[article_id] .'">' . $row[article_title] . '</td><td>' . $row[timestamp] . '</td></tr>'; print "\n"; } ?> but when i click the link, it goes to the right edit page but the text fields are blank for example, the first entry in the db, id=1 the web address says example.com/admin/editform/index.php?id=1 i would expect these text fields to be filled out with this code <?php //this page edits articles session_start(); require ('../../../install/PEAR/DB.php'); require ('../../../../dbfiles/db_login.php'); require ('../requires/formhelpers.php'); require ('../requires/timearrays.php'); $db->setErrorHandling(PEAR_ERROR_DIE); $db->setFetchMode(DB_FETCHMODE_ASSOC); //grabs article_id for later query $id = $_GET['id']; if ($_SESSION['username']){ if($_POST['_submit_check']){ if($form_errors = validate_form()){ show_form($form_errors); } else { process_form(); } } else { show_form(); } } else { print '<a href="../index.php">Log In dummy</a>'; } function show_form($errors = '') { global $db, $id; $items = $db->getRow('SELECT article_title, article FROM text_cms WHERE article_id = ?', array($id)); $items['article_title'] = $title; $items['article'] = $article; //If form has been submitted, get defaults from submitted variables if ($_POST['_submit_check']){ $defaults = $_POST; } else { $defaults = array('title' => $title, 'body' => $article); } if ($errors){ print 'Please correct these errors: <ul><li>'; print implode('</li><li>', $errors); print '</li></ul>'; } print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">'; //begin the unique form print '<br />'; print "Article Title"; print "<br />\n"; input_text('title', $_POST); print "<br /><br />\n"; print 'Article Body'; print "<br />\n"; input_textarea('body', $_POST); print "<br /><br /><br />\n"; input_submit('submit', 'Edit Article'); print '<input type="hidden" name="_submit_check" value="1" />'; print "\n"; print '</form>'; } function validate_form(){ if (trim(strlen($_POST['title'])) > 255){ $errors[] = 'Article title is too long, please shorten it'; } return $errors; } function process_form(){ global $db, $id, $title, $article; $edited_title = $_POST['title']; $edited_article = $_POST['body']; //check to see if title has changed, if changed, update db if ($edited_title != $title){ $db->query('UPDATE text_cms SET article_title = $edited_title WHERE article_id = $id'); print "Updated <strong>$title</strong> in the database.<br />\n"; } //check to see if article has changed, if changed, update db if ($edited_article != $article){ $db->query('UPDATE text_cms SET article = $edited_article WHERE article_id = $id'); print "Updated <strong>article body</strong> in the database.<br />\n"; } print '<a href="../managearticle/index.php">Go back to article management</a>'; } ?> where did I go wrong thanks ps. I am not trying to be too secure right now, just trying to get things working, will clean up security issues later Quote Link to comment https://forums.phpfreaks.com/topic/108923-solved-displaying-database-field-values-in-html-form-fields/#findComment-558870 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.