PHP Problem

stevet70

New Member
Messages
35
Reaction score
0
Points
0
So close, yet so far

I'm attempting to develop a series of pages which display text from a database and have an edit button. When you click on the edit button the text becomes editable (using TinyMCE).

The code for editing / updating the text works when the edit button isn't included. The code for including the edit button and switching to an editable version of the page also works.

The problem is, when you click on edit and update the text it isn't actually updating the text in the database - it just gives you what was there originally.

Here's what I have above the doctype to set up the edit / preview button

<?php
// process the script only if the form has been submitted
if (array_key_exists('edit', $_POST)) {
// start the session
session_start();
$_SESSION['authenticated'] = 'Edit';
}
// if the session variable has been set, show editable content
if (isset($_SESSION['authenticated'])) {
$view = 'Editable';
}
// if the session variable hasn't been set, leave as preview
else {
$view = 'Preview';
}
?>

Then in the body I first include the edit button

<?php
if ($view == 'Preview') {
include('edit_button.php');
}
?>

followed by the database connection and the options for regular or editable versions of the text

<?php include('db_connect.php'); ?>

<?php
if ($view == 'Preview') {
include('db_preview.php');
}
else {
include('db_edit.php');
}
?>

the code in db_edit.php is as follows:

<?php
if (isset($_POST['text'])):
// The text have been updated.
$text = $_POST['text'];
$sql = "UPDATE page_text SET text='$text' WHERE id='$id'";
if (@mysql_query($sql)) {
echo $text;
mysql_close();
} else {
echo '<p>Error updating text: ' .
mysql_error() . '</p>';
}
else: // Allow the user to edit text
$page_text = @mysql_query(
"SELECT text FROM page_text WHERE id=\"$id\"");
if (!$page_text) {
exit('<p>Error fetching text: ' .
mysql_error() . '</p>');
}
$page_text = mysql_fetch_array($page_text);
$text = $page_text['text'];

// Convert special characters for safe use
// as HTML attributes.
$text = htmlspecialchars($text);
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>
<textarea id="elm1" name="text" cols="50" rows="10"><?php echo $text; ?></textarea>
</label><br />
<input type="submit" value="SUBMIT" />
</form>
<?php endif; ?>


Strangely, if I remove the if / else statement for which version of the text to display and just include db_edit.php, I can update the text and click on submit - which then displays the updated text along with the edit button. I can then click on edit to go back to the editable version and make further changes.

My guess is that by having 2 bits of code that rely on different $_POST statements there's a clash and they aren't being differentiated between?
But if that is the case I don't know how to get around it

Any ideas?

thanks
Steve
 

VPmase

New Member
Messages
914
Reaction score
0
Points
0
It could be in your db_edit.php or db_preview or another include.
 

stevet70

New Member
Messages
35
Reaction score
0
Points
0
That's the tricky thing, all the different elements work in isolation or to a large extent when brought together and I'm not getting any error messages.

The only obvious thing is that having 2 form buttons, even though they're differently named, is messing things up:

if (array_key_exists('edit', $_POST)) {

if (isset($_POST['text'])):

Can PHP not handle 2 form buttons designed to do different things within the same page?
Is there another way of doing it?
 

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
sorry if this is a stupid question, but when you say 'form button', you mean just like a submit button, right? if that's the case, i can't see how there would be a problem.
 

stevet70

New Member
Messages
35
Reaction score
0
Points
0
Not at all stupid, they're exactly that, both are submit buttons, except one is labelled Submit and the other Edit.

They are both named differently within their form make up and each $_POST statement relates to that - or at least I think so - which is where the problem seems to be and it's got me stumped (not that it takes much!)
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
I would say, use AJAX.
 
Top