+ Reply to Thread
Results 1 to 6 of 6

Thread: PHP Problem

  1. #1
    stevet70 is offline x10Hosting Member stevet70 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    35

    PHP Problem

    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

  2. #2
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: PHP Problem

    It could be in your db_edit.php or db_preview or another include.

  3. #3
    stevet70 is offline x10Hosting Member stevet70 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    35

    Re: PHP Problem

    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?

  4. #4
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: PHP Problem

    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.

  5. #5
    stevet70 is offline x10Hosting Member stevet70 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    35

    Re: PHP Problem

    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!)

  6. #6
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP Problem

    I would say, use AJAX.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  2. problem in php mail function and smtp
    By idrees in forum Free Hosting
    Replies: 2
    Last Post: 02-08-2008, 05:16 PM
  3. Sigo con problemas con phpbb2
    By reciecho in forum Soporte
    Replies: 7
    Last Post: 10-20-2007, 06:28 PM
  4. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM
  5. php ad code problem
    By sourabhj in forum Free Hosting
    Replies: 7
    Last Post: 08-22-2006, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers