Jump to content

[SOLVED] creating an 'edit' page $_GET question


Lodius2000

Recommended Posts

my blog cms is coming along nicely, but my edit page needs help,

 

on the page where you view a list of rows in the database, i have this code to display some of the row's fields and link each row to the edit page so that the row's content can be altered

 

<?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";
}
?>

 

this works perfectly in the url, for article_id=1 the web address reads /editform/index.php?id=1

 

but

 

what I want is for the text and textarea on the edit page to be filled out with the contents of the correct row

 

here is my edit page code

<?php

//this page edits articles
session_start();
require ('../../../install/PEAR/DB.php');
require ('../../../../dbfiles/db_login.php');
$db->setErrorHandling(PEAR_ERROR_DIE);
$db->setFetchMode(DB_FETCHMODE_ASSOC);

//print a text box
function input_text($element_name, $values){
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '"/>';
}

//print a textarea
function input_textarea($element_name, $values){
print '<textarea cols="75" rows="50" name="' . $element_name .'">';
print htmlentities($values[$element_name]) . '</textarea>';
}



//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>';

}

?>

 

this prints the page right but the fields are empty

 

I know almost nothing about $_GET

 

please let me know what I did wrong

 

Thanks in advance

UPDATE:

 

i have made sure that the variable's $title and $article are not empty by echoing them

 

so I guess my question is what is wrong with my script (Above) that they wont become defaults, so that I can edit them

 

thankyou

in function show_form change part

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";

to

print '<br />';
print "Article Title";
print "<br />\n";
input_text('title', $default);
print "<br /><br />\n";
print 'Article Body';
print "<br />\n";
input_textarea('body', $default);

print "<br /><br /><br />\n";

it does contain the correct ID, also i have checked the other variables, $title and $article, but they wont show up in the form,

 

i can print them

 

print $title;

 

but when i try to make them the default for the form field they dont show up

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.