Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.