ronoc_php Posted December 18, 2011 Share Posted December 18, 2011 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 https://forums.phpfreaks.com/topic/253423-newb-question/ Share on other sites More sharing options...
melloorr Posted December 18, 2011 Share Posted December 18, 2011 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. I don't understand what you mean. Could you give an example? Link to comment https://forums.phpfreaks.com/topic/253423-newb-question/#findComment-1299017 Share on other sites More sharing options...
ronoc_php Posted December 18, 2011 Author Share Posted December 18, 2011 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. Link to comment https://forums.phpfreaks.com/topic/253423-newb-question/#findComment-1299022 Share on other sites More sharing options...
melloorr Posted December 18, 2011 Share Posted December 18, 2011 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 https://forums.phpfreaks.com/topic/253423-newb-question/#findComment-1299027 Share on other sites More sharing options...
ronoc_php Posted December 18, 2011 Author Share Posted December 18, 2011 I did that no luck Link to comment https://forums.phpfreaks.com/topic/253423-newb-question/#findComment-1299032 Share on other sites More sharing options...
melloorr Posted December 18, 2011 Share Posted December 18, 2011 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 https://forums.phpfreaks.com/topic/253423-newb-question/#findComment-1299036 Share on other sites More sharing options...
melloorr Posted December 18, 2011 Share Posted December 18, 2011 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 https://forums.phpfreaks.com/topic/253423-newb-question/#findComment-1299038 Share on other sites More sharing options...
ronoc_php Posted December 18, 2011 Author Share Posted December 18, 2011 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 https://forums.phpfreaks.com/topic/253423-newb-question/#findComment-1299046 Share on other sites More sharing options...
melloorr Posted December 18, 2011 Share Posted December 18, 2011 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 https://forums.phpfreaks.com/topic/253423-newb-question/#findComment-1299055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.