Page 1 of 1

Help needed with Sessions and Array stuff.

Posted: Fri Mar 20, 2009 11:38 am
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.

Re: Help needed with Sessions and Array stuff.

Posted: Fri Mar 20, 2009 12:22 pm
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']

Re: Help needed with Sessions and Array stuff.

Posted: Fri Mar 20, 2009 12:34 pm
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

Re: Help needed with Sessions and Array stuff.

Posted: Fri Mar 20, 2009 12:48 pm
by Daómor
Its now working after I used your method.

Thanks again!

That has taught me alot.

Cheers