Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Member Taffu's Avatar
    Join Date
    Dec 2006
    Posts
    34
    Member #
    14486
    I have a specific goal in mind. I'm trying to create an Edit page in the Administration area of my project where I use a GET feature to input that existing values of the article into a form. Everything works fine, until I go to actually update it through submitting. Here's my code:
    PHP Code:
    <?php
    if(isset($_GET['id'])) 
    {
    include 
    'config.php';
    include 
    'opendb.php';

    $id $_GET['id'];

    $query "SELECT * FROM pg_news WHERE id=$id"
    $result mysql_query($query)or die(mysql_error()); 
    $row mysql_fetch_assoc($result);

    {echo 
    "
    <table cellspacing='0' cellpadding='2' border='0'>
    <form method='post' action='edit_news.php'>
    <tr>
    <td class='formtd'>Title: &nbsp;</td><td class='formtd'><input type='text' class='textinput' id='title' name='title' value='
    {$row['title']}'></td>
    </tr>
    <tr>
    <td class='formtd'>By: &nbsp;</td><td class='formtd'><input class='textinput' type='text' id='postedby' name='postedby' value='
    {$row['postedby']}'></td>
    </tr>
    <tr>
    <td valign='top' class='formtd'>Body: &nbsp;</td><td class='formtd'><textarea class='areainput' id='content' name='content' cols='40' rows='10' wrap='virtual'>
    {$row['content']}</textarea></td>
    </tr>
    <tr>
    <td></td><td class='formtd'><input type='submit' class='formbut' id='post' name='post' value='Post News'></td>
    </tr> 
    </form>
    </table>
    "
    ;}

    include 
    'closedb.php';
    }
    elseif(isset(
    $_POST['post'])) 

    include 
    'config.php';
    include 
    'opendb.php';

    $title $_POST['title'];
    $postedby $_POST['postedby'];
    $content $_POST['content'];

    $query "UPDATE pg_news SET title='$title', postedby='$postedby', content='$content' WHERE id={$row['id']}";
    mysql_query($query) or die(mysql_error());

    include 
    'closedb.php';
    echo 
    "<center>Your article has been updated!<br><a href='admin_news.php'>Back to News Administration</a></center>";

    ?>
    I think my hangup is where the {$row['id']} pulls on the "elseif" portion. I don't know how to get it to work though. It will give me a parse error on submission, but it only gives me a "near " at line 1" error output, which doesn't really narrow down where the error is coming from.
    I've tried this completely closing the first string with an "}else {" and isolating the second portion of the script, but that doesn't work either. If I remove the action on the form, I simply get a return to the original GET portion, which doesn't update on the POST and simply activates the first portion of the script and I'm right back where I started before I hit Submit.
    Any ideas?
    Owner - http://www.project-guild.com (in development)

  2.  

  3. #2
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    Hang on a sec.. it's usually a better idea to either give a single global GET variable for determining what to do (like, do=editnews, do=deletepage, etc), or using separate files. Just to make it cleaner and relatively simpler.

    I can't find anything wrong, though, at least not yet.

    Do keep in mind dangers of infiltration. Do a check for admin status first, and make sure to escape the strings (check if magic quotes are on).
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  4. #3
    Member Taffu's Avatar
    Join Date
    Dec 2006
    Posts
    34
    Member #
    14486
    That makes sense. Now if I do that (ie. use a global GET, like "editnews.php?do=edit" and "editnews.php?do=update" for instance), I'm not sure how I would build it where it will retain the original ID I'm trying to update.
    Would it be like:
    editnews.php?id=id?do=edit?
    And the admin area I'm working on isn't live yet, I haven't even built the login/session system yet. I want to make sure all features are entirely functional before even testing it live (which obviously they all aren't yet...lol).
    If I use seperate files I run into the same hitch, cause when I use a query to return the existing data in that row, I still have to try and use GET & POST in the same script. It's reacting as if it's not recognizing the (isset($_POST['post'])), or simply the query in that portion of the script. I think...if that makes sense at all.
    Owner - http://www.project-guild.com (in development)

  5. #4
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    I meant to use a central editing file... like adminprocess.php. You can then introduce GETs to determine which action to execute, and POSTs for the data. It's quite simple. For example, adminprocess.php?do=editpage directs to the editpage switch case in the file, and then ID, Content, etc is pulled out of POST data. That means the submit form would be like

    HTML Code:
    <form action="adminprocess.php?do=editpage" method="POST">
    <input type="text" name="title" />
    <input type="hidden" name="id" value="<?=$_GET['editid']?>" />
    </form>
    And so forth.

    I suppose you know that you can combine multiple get variables, like adminprocess.php?do=editpage&authorization=editor& something=what.
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  6. #5
    Member Taffu's Avatar
    Join Date
    Dec 2006
    Posts
    34
    Member #
    14486
    Ahh, I understand now. I'll give this a whirl and see if it works. Thanks for the tip!
    Owner - http://www.project-guild.com (in development)

  7. #6
    Member Taffu's Avatar
    Join Date
    Dec 2006
    Posts
    34
    Member #
    14486
    Working like a charm now! Thanks for the input & tips, Steax
    Owner - http://www.project-guild.com (in development)

  8. #7
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    No problem, good luck!
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:20 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com