Jump to content

newb question


ronoc_php

Recommended Posts

Hey i am doing a login and blog system i have done the login and reg part but i am having troubles with the adding post. Basically at the moment my user can make a post and they can anyone can  view it, but i need to the user only be able to create there own prv post and that when anybody else logs in they cant  make a post under it. Does anyone have any ideas?

 

this is my code for the posts

 

 

<?php

session_start();

 

include('db_connect.php');

?>

Welcome to the fear blog          <a href="logout.php">log out</a><hr/>

<?php

$sql = mysql_query("SELECT * FROM posts ORDER BY id ");

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

$title = $row['title'];

$content = $row['content'];

$category = $row['category'];

?>

 

<div id="post">

<div id="wrapper">

 

<div id="title"> <label>Title</label> <?php echo $title; ?> </div>

 

<div id="category"><label>category</label> <?php echo $category; ?> </div>

 

 

<div id="content"> <label>Content</label><?php echo $content; ?> </div>

</div>

 

<?php

}

?>

 

 

    <div id="contents">

  <form action="post.php" method="post">

  <label> Title:</label><input type="text" name="title" /><br/>

<label> Category:</label><input type="text" name="category" /><br />

<label> Content:</label><textarea name="content"></textarea><br/>

<input type="submit" name="submit" value="Post"/>

 

 

  </form>

  </div>

  </div>

</div>

</div>

Link to comment
Share on other sites

So basically anyone that logs in can only see there own post and nobody else, So other users should not see any other comments bare there own.

 

Store the username in the database along with the post, (and also in a cookie or a session or something) then:

("SELECT * FROM posts WHERE username = '$username' ORDER BY id ");

 

Something like that?

Link to comment
Share on other sites

When they log in,set a cookie

setcookie(username, $username, time() 3600);

 

If you are storing their username in the database, next to their post, this this SHOULD work:

<?php
session_start();

include('db_connect.php');
?>
Welcome to the fear blog          <a href="logout.php">log out</a><hr/>
<?php

$username = $_COOKIE["username"];
   
$sql = mysql_query("SELECT * FROM posts WHERE username = '$username' ORDER BY id ");
while($row = mysql_fetch_array($sql)){
   $title = $row['title'];
   $content = $row['content'];
   $category = $row['category'];
?>

<div id="post">
<div id="wrapper">

<div id="title"> <label>Title</label> <?php echo $title; ?> </div>   

<div id="category"><label>category</label> <?php echo $category; ?> </div>


<div id="content"> <label>Content</label><?php echo $content; ?> </div>
</div>

<?php
}
?>


    <div id="contents">
   <form action="post.php" method="post">
  <label> Title:</label><input type="text" name="title" /><br/>
<label> Category:</label><input type="text" name="category" /><br />
<label> Content:</label><textarea name="content"></textarea><br/>
<input type="submit" name="submit" value="Post"/>


   </form>
   </div>
   </div>
</div>
</div>

 

But this is just very basic, you should probably have more security, and you will probs get errors if they are not logged in

Link to comment
Share on other sites

This code will get rid of the errors, and I also noticed that your form action was "post.php" (which I am assuming is the very same file) so I have changed that too:

<?php
session_start();

include('db_connect.php');
?>
Welcome to the fear blog          <a href="logout.php">log out</a><hr/>
<?php

if(isset($_COOKIE["username"];
{
$username = $_COOKIE["username"];
   
$sql = mysql_query("SELECT * FROM posts WHERE username = '$username' ORDER BY id ");
while($row = mysql_fetch_array($sql)){
   $title = $row['title'];
   $content = $row['content'];
   $category = $row['category'];
?>

<div id="post">
<div id="wrapper">

<div id="title"> <label>Title</label> <?php echo $title; ?> </div>   

<div id="category"><label>category</label> <?php echo $category; ?> </div>


<div id="content"> <label>Content</label><?php echo $content; ?> </div>
</div>

<?php
}
}
?>


    <div id="contents">
   <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
  <label> Title:</label><input type="text" name="title" /><br/>
<label> Category:</label><input type="text" name="category" /><br />
<label> Content:</label><textarea name="content"></textarea><br/>
<input type="submit" name="submit" value="Post"/>


   </form>
   </div>
   </div>
</div>
</div>

Link to comment
Share on other sites

Thanks for getting back, sorry to ask, But what do u mean if i am storing the username next to the posts. What i did was make a table named posts and another called members any user that is signing up is given an id number, as far as errors its fine i'm doing this as part as an assignment for college, there not expecting alot!!

Link to comment
Share on other sites

Well, when you store the post in the database, store the username at the same time. For example:

("INSERT INTO posts (id, post, username) VALUES ('$id, $post, $username' ");

Something along the lines of that. So when you store the username in the cookie and use the other code I posted, it checks that username against the database, and returns any post that was created by that user

Link to comment
Share on other sites

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.