Help needed with Sessions and Array stuff.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Daómor
Forum Newbie
Posts: 3
Joined: Fri Mar 20, 2009 11:24 am

Help needed with Sessions and Array stuff.

Post by Daómor »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi there,

I'm fairly new to PHP and I am trying to make a simple blog, that allows me to edit posts that have been made.

Currently I have managed to make it so that when I click edit of a certain post it sends the first entry from my database to the edit.php page. How can I send the correct message to the edit.php page?

I have done some research into multidimensional arrays, but was unsure if this was the right direction to go in, and after trying to get my head around them I figured I'd ask for help.

blog.php

Code: Select all

            <?php
                include("wdc.php");
 
                $query="SELECT * FROM `blog` ORDER BY `date` DESC";
                $result=mysql_query ($query)
                        or die("SQL failed");
                        
                
                if(mysql_num_rows($result)==0)
                {
                    echo"There are no messages";
                    echo"<p><a href='index.php'>Click here</a> to go home.</p>";
                }
                else
                {       
                    while ($row=mysql_fetch_array($result)) 
                    {
 
                            echo "<table class='blogtable'><tr><td class='blogdate' colspan='2'>Date: ".$row["date"]."</td></tr>";
                            echo "<tr><td class='bloguser'><font color='".$row["color"]."'><b>".$row["username"]."</b></font></td>";
                            echo "<td class='blogmessage'>".nl2br($row["message"])."<br/><br /><a href='edit.php'>[edit]</a></td></tr>";
                            echo "</table>";
                            
                            $m = $row["message"];
                            $_SESSION["edit"] = $m;
                    }
                }
                mysql_close($conn);         
            
            ?>

edit.php:

Code: Select all

<?php
 
                    if ( !isset ($_SESSION["gatekeeper"]))
                    {
                        echo "You're not logged in. Go away!";
                    }
                    else
                    {
                        echo "Hi " .$a.", please edit your message and click 'Update Message'." ; 
                     
                    
                                
                            echo "<form action='updateblog.php' method='POST'>";
                                echo "<table class='messagetable'>";
                                    echo "<tr>";
                                        echo "<td width='157'>Message:</td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                        echo "<td width='157'><textarea class='textarea' name='blogmessage'>".$m."</textarea></td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                        echo "<td><input type='submit' value='Update Message' /></td>";
                                    echo "</tr>";
                                echo "</table>";
                            echo "</form>";
                                
                                echo" <a href='blog.php'>View Blog</a>";
                                
                    }
?>
Any help would be greatly appreciated!

Cheers


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Help needed with Sessions and Array stuff.

Post by pickle »

You need to call session_start() at the beginning of both those files. $_SESSION isn't available without doing so.

I'd suggest rather than storing the entire contents of the blog post in $_SESSION, that you just include the id of the blog post in the URL when creating the link. Then in edit.php, you look up the blog post based on that id.

So, the link would look like edit.php?id=2, and you'd retrieve that in edit.php by accessing $_GET['id']
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Daómor
Forum Newbie
Posts: 3
Joined: Fri Mar 20, 2009 11:24 am

Re: Help needed with Sessions and Array stuff.

Post by Daómor »

Sorry perhaps I should have posted the whole file. I do have session_start() at top of the files. I just posted the main PHP chunks.

Thanks for your approach, I will give that a try.

Cheers
Daómor
Forum Newbie
Posts: 3
Joined: Fri Mar 20, 2009 11:24 am

Re: Help needed with Sessions and Array stuff.

Post by Daómor »

Its now working after I used your method.

Thanks again!

That has taught me alot.

Cheers
Post Reply