I have the following code:
while ($content = mysql_fetch_array($content2)){
echo $content[0];
session_start();
$_SESSION['content'] = $content[0];
?>
<br />
<a href="edit.php"> Edit post. </a>
<br /> <br />
<?php
}
It fetches and displays all posts from a blog system database, and you can click on “Edit.” below each post. What I want to do is pass the content of that post to my edit.php script.
The problem is, whichever “Edit” button I click on, this will always pass the content of my last post, not the content of the post above that “Edit” button.
Now, I can see why that might be: The whole loop will be executed before I click on any button, and the value stored in $_SESSION[‘content’] will always be the value from the loop’s last iteration. Am I right?
Maybe I shouldn’t be using SESSION. Is there a better approach? How can I pass the “right” value of $content[0] to my edit.php script?
The reason why the edit button is always getting the last post is because you are overriding the content variable in the session on each loop.
I am not sure what information you are getting from the database, but you should have a unique ID in the database for each post which you grab in the array as well. If this does not exists, I would strongly recommend making a change in your table as it will simplify your process.
You will then have to set up the edit page to grab the id
$_GET['postId']and grab the proper content from the database.