Jump to content

[SOLVED] Form/switch problem (case in PHP 5?)


Recommended Posts

Okay so I made a forum on my site.  I had all the basic functions working, including editing.  But when I went back and added a bbcode function today, I found out that I couldn't edit posts anymore.  I know it's not a database connection problem because I can add new threads and replies, I just can't edit them anymore.  If someone could find out what's going on I'd love some feedback.

 

<?php session_start();
include('db_config.php'); //gets the database information
$pagetitle = 'Edit Post';
include('header.php');
if(!empty($_SESSION['user'])) { //makes sure the user is logged in
   
        if (!isset($mode) || empty($mode) || (!$mode)) 
            {  
            $mode = "display";  //if there's no mode, set default to display
                }      
        
        switch($mode) { 
            case "display": 
                // first, get the info from the database

            $id = ($_GET["id"]);  //get the reply id number

            $q="SELECT * FROM forum_answer where a_id='$id' limit 1"; 
            $result = mysql_query($q) or die(mysql_error());  //gets the information from the database

            while ($row=mysql_fetch_array($result)) 
                { 

                    $ida=$row["a_id"]; 
                    $entry=$row["a_answer"];
                    //include the edit form
?> 
                
                    <form action="editanswer.php?mode=update&id=<?php echo $ida; ?>" method="post"> 
                    <table width="130" border="0" cellspacing="1" cellpadding="0"> 
                    <tr> 
                        <td height="23" colspan="2"><b>Edit Post</b></td> 
                    </tr>
                    <tr> 
                        <td height="25">Text:</td> 
                        <td height="25"> 
                        <textarea name="newentry" cols="30" rows="5"> <?php echo "$entry"; ?></textarea> 
                        </td> 
                    </tr> 
                    <tr><td colspan=2><input type="submit" value="submit"  name="submit"></td></tr> 
                    </table></form> 
                     
                    <?php  
                
                } // end of while loopand the mode display
                
            break; 
            
            case "update":
                $id = $_GET["id"];  //get the reply id
                $newentry = $_POST["newentry"]; //get the new entry
                $datetime=date("m/d/y h:i A"); //date of edit
                $editor=$_SESSION['user']; //person who edited
            
                $q="UPDATE  forum_answer SET a_answer='$newentry', editid='$editor', edittime='$datetime' WHERE a_id='$id'"; 
                $result = mysql_query($q) or die(mysql_error());  //add it all into the database
                 
                if ($result) { //if everything updates correctly, display a thank you and a link to return to the thread.
                    $query=mysql_query("SELECT * from forum_question where id='$id'") or die(mysql_error());
                    $return=mysql_fetch_array($query);
                    echo 'Thank you, entry has been updated.<a href="view_topic.php?id='.$return['id'].'">Go back?</a>'; 
                }
                else {print 'There was an error.  Please contact an admin.'; //if there is no update, display this error message
}
                
                break; 

} // end of switching.

}
else {$_SESSION['user']='';

if(empty($_SESSION['user'])) {print "You aren't logged in!"; }
}
include('footer.php');

?>

 

I added in an else statement after the database update to see if that was the problem and it isn't.  When the page goes from mode=display to mode=update nothing seems to happen anymore.  It just refreshes the page and puts the old information back in the textbox.  I've checked the database and it hasn't updated.

 

I'm not sure if this will help, but my server recently upgraded to PHP 5 from PHP 4.  But I don't know what that would do to this.  It worked perfectly in PHP 4 but I don't see how any of this isn't PHP 5 compatible.

 

Any help would be lovely =)

Link to comment
https://forums.phpfreaks.com/topic/83883-solved-formswitch-problem-case-in-php-5/
Share on other sites

The update case isn't executing.  I tried adding a simple echo statement to the very beginning of the update case and nothing happens.  It doesn't show.  And how much more would I comment it?  It's pretty self-explanatory.

 

And going through more pages on my site...none of the editing features work and they all have this coding framework (using switch with two cases).  Is there some sort of change I need to know about from PHP 4 to 5?

The link to the page includes mode=display and at the very beginning I set the mode to display with:

if (!isset($mode) || empty($mode) || (!$mode)) 
            {  
            $mode = "display";  //if there's no mode, set default to display
                }   

 

So that's not the problem.  The form and page display, but don't update.

 

If you mean where do I set the case for update?  It's right after the line case "update":

They mean comment out the code, as it, ignore it, and then echo the result so you can see.  Not add more comments to what it's doing.

 

The update case isn't executing.  I tried adding a simple echo statement to the very beginning of the update case and nothing happens.  It doesn't show.  And how much more would I comment it?  It's pretty self-explanatory.

 

I had a similiar problem this weekend with a line just like this

 

if(!empty($_SESSION['user']))

 

What I did was change it so when they logged in, $_SESSION['auth']=1

 

Then I checked

 

if ($_SESSION['auth'] !=1)

 

and that solved my problem.

That isn't the problem!  There is no problem with the login system.  The problem is with the switch and cases.  I don't want to use if statements for a reason.  I want to use switch.  That doesn't help the switch system at all.

 

I commented out the first statement that defines which case to start with, and guess what.  I got an error that $mode didn't exist.  There's nothing wrong with my definition of $mode.

 

I use that if statement on every one of my pages, and if that hadn't worked right then I wouldn't have been able to even view the forums.  I don't see how it applies.

Your code is dependent on register globals being on, which is sad really because php.net turned them off by default in 2002 and no new code should have been written after that point in time that relied on register globals being on.

 

Add this line of code -

 

$mode = $_GET['mode'];

 

What the posts above were trying to get you to do was to echo the variable you are testing to see what it actually contains. Too many people have conditional tests that fail then think someone else can tell them why, without it occurring to them to "look" at the value that is being tested to see what it actually is.

 

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.