Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts

    Admin Panel fopen & fwrite

    Hi,

    I'm having trouble getting the fopen & fwrite functions to work. I've included them in an admin panel script to make for easy, online editing. For some reason, the info isn't getting passed, and I don't know why. I think it may have to do with $_GET & $_POST conflicting with each other.

    What happens is I click the link for the page I want to edit, it shows the form and I click the 'Update' button and the page returns blank. It doesn't even echo that it failed.
    Anyone have an idea? I don't

    PHP Code:
    <?php
    session_start
    ();
    if(@
    $_SESSION['admin'] != "yes"){
        echo 
    "You are not admin.";
    }else{
    include(
    "/home/flawclan/public_html/config.php");
    $connect mysql_connect("$host","$user","$passwd");

    mysql_select_db("$dbname",$connect);

    $table_name "cms"//table
    //navbar query
    $page $_GET['page'];
    $sql "SELECT * FROM $table_name";
    $query mysql_query($sql,$connect);
        while(
    $row = @mysql_fetch_array($query)){
            
    $id $row['content_id'];
            
    $title $row['page_title'];
            echo 
    "<a href=\"admincp.php?page=".$id."\">Edit $title page</a><br />";
        }
        
    if(
    $page == "1"){
        
    $pagename "main.php";
    }
    if(
    $page == "2"){
        
    $pagename "news.php";
    }
    if(
    $page == "3"){
        
    $pagename "roster.php";
    }
    if(
    $page == "4"){
        
    $pagename "downloads.php";
    }
    if(
    $page == "5"){
        
    $pagename "server.php";
    }
    if(
    $page == "6"){
        
    $pagename "about.php";
    }
    //content page query
    $sql "SELECT content_id FROM $table_name WHERE content_id='$page'";
    $query mysql_query($sql,$connect);
        while(
    $row = @mysql_fetch_array($query)){
                switch(
    $_POST['button']){
                    case 
    "Update":
                        
    $file fopen("/home/flawclan/public_html/pages/".$pagename."""w");
                        
    $content $_POST['content'];
                        
    $write fwrite($file,$content);
                        if(
    $write){
                            echo 
    "Updated sucessfully";
                        }else{
                            echo 
    "Darn it... something went wrong";
                        }
                        
    fclose($file);
                break;
                default:
                        echo 
    "<center><p>Editing $pagename";
                        echo 
    "<form action=";
                        echo 
    "admincp.php";
                        echo 
    " method=\"post\">
                              <textarea name=\"content\"></textarea><br />
                              <input type=\"submit\" value=\"Update\" name=\"button\"></form>"
    ;
            }
        }
    }
    ?>
    Thanks for any possible help!

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Well, I'm not seeing any error checking on either the variables passed in to the script or the queries.

    1. Try echoing $page to see if it contains a value you expect
    2. for all queries, add "or die(mysql_error());" like this:
    PHP Code:
    $query mysql_query($sql,$connect) or die(mysql_error()); 

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    yeah it is echoing the variable I expect it to be, I checked just as you suggested. I tried copying and pasting
    PHP Code:
    <?php
       
    switch($_POST['button']){
                    case 
    "Update":
                        
    $file fopen("/home/flawclan/public_html/pages/".$pagename."""w");
                        
    $content $_POST['content'];
                        
    $write fwrite($file,$content);
                        if(
    $write){
                            echo 
    "Updated sucessfully";
                        }else{
                            echo 
    "Darn it... something went wrong";
                        }
                        
    fclose($file);
                break;
                default:
                        echo 
    "<center><p>Editing $pagename";
                        echo 
    "<form action=";
                        echo 
    "admincp.php";
                        echo 
    " method=\"post\">
                              <textarea name=\"content\"></textarea><br />
                              <input type=\"submit\" value=\"Update\" name=\"button\"></form>"
    ;
            } 
    ?>
    into another file and I ran it from there and it worked fine. The page was updated and it echoed that it was indeed successful. I have one idea maybe why its not working, because the db query isn't letting anything else be written to the page? I don't know at this point. I'm just throwing ideas around.

    *EDIT*
    Ok I fixed it! After hours of trouble shooting I realized the problem lay in this line here:
    PHP Code:
    <form action="admincp.php" method="post"
    The page wasn't right! I changed it to:
    PHP Code:
    <form action="admincp.php?page=$page" method="post"
    Thanks for the help! Simple mistakes always take the longest to find and fix.
    Last edited by fl00d; 08-02-2007 at 01:37 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
    •