0

I've made a blog that prints all the posts from a single table named BlogData, the problem is that I want to be able to edit only the posts that I've posted, so I have stored in the column "poster" the name of the username that made the post.

 <div class="container">
 <?php
 $sql = mysql_query("SELECT * FROM BlogData ORDER BY id DESC");
 while($row = mysql_fetch_array($sql)){
    $title = $row['title'];
$content = $row['content'];
$category = $row['category'];
$image = $row['id'];
$extension= $row['ext'];
$title_color = $row['title_color'];
$content_color = $row['content_color'];
$category_color = $row['category_color'];
 /////////////////////////////////////////////////////
$poster = $row['poster'];  // THIS IS WHERE THE USERNAME IS STORED
 /////////////////////////////////////////////////////
 ?>
 <hr />
 <div class="title" style=color:#<? echo $title_color ?>>
 <?php echo $title; ?>
 </div>
 <div class="date" style=color:#<? echo $category_color ?>>
 <?php echo $category; ?>
 <div class="poster" style=color:#<? echo $poster_color ?>>
 <?php echo $poster; ?>
 </div>
 </div>
 <?php 
 echo "<img width='1140' height='300' src='uploads/".$image.".".$extension."'>";
  ?>
 <br>
 <div class="content" style=color:#<? echo $content_color ?>>
 <?php echo $content; ?>
 </div>
 <br>
 ////////////////////////////////
 ////////////////problem here
 ////////////////////////////////
 <?php
 if ($_SESSION['name'] == $row['poster']){
 echo "1";
 } else {
 echo "2";
 }
 }
 ?>
 <?php
 }
 ?>

the problem is, the "echo" on the end of the code, is printing 1 on all posts that have something stored on the row "poster" and 2 on all the posts that the row "poster" is empty, it should print 1 only on the posts that I've created. how can this be solved?

1
  • where is the value of $_SESSION['name'] is set? Commented Nov 30, 2014 at 9:05

1 Answer 1

1

Add

session_start();

In your pup code before reading the $_SESSION variable.

PHP session_start manual

You could try to change your IF statement in this way, too:

if (isset($_SESSION['name']) && $_SESSION['name'])
Sign up to request clarification or add additional context in comments.

6 Comments

I have this command on the top of the page, do I need to put it again? still same problem
I can't see it in your posted code. If it is at the beginning it is ok. What happens if you echo $_SESSION[name]?
echoed $_SESSION[name] on if and else, and both printed "1"
I think you have to review the page in which you save the $_SESSION['name'] variable.
pastebin.com/RgkDCJen and it stores exaclty the user that is logged in/posting...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.