February 21, 201115 yr Hi all, I have built a comment system on my website and i want it to display the users email address that posted the comment, can anyone advise. <?php //connect to database $mysqli = mysqli_connect("connection info is here"); //get items $sql_get_query = "SELECT title, comment, time FROM blog_pettit"; $sql_get_results = mysqli_query($mysqli, $sql_get_query) or die(mysqli_error($mysqli)); $email_query = "SELECT email FROM users"; $email = $_SESSION['email']; if (mysqli_num_rows($sql_get_results) < 1) { $display_comments = "<p><em>There are currently no comments in the database. </em></p>"; } else { while ($comments = mysqli_fetch_array($sql_get_results)) { $title = $comments['title']; $comment = $comments['comment']; $time = $comments['time']; $display_comments .= " <div class=\"comment_holder\"> <div class=\"comment_title\">".$title."</div> <div class=\"comment_email\">".$_SESSION['email']."</div> <div class=\"comment_stamp\">".$time."</div> </div> <br /> <div class=\"comment_comment\">".$comment."</div> "; } } ?> <?php echo $display_comments; ?>
February 22, 201115 yr Session would be a pretty rubbish way to do it. A session variable is unique to machine that runs the script unless you are going to initialise every e-mail address in your database as a session variable. That would suck.
February 22, 201115 yr Author Session would be a pretty rubbish way to do it. A session variable is unique to machine that runs the script unless you are going to initialise every e-mail address in your database as a session variable. That would suck. what would you reccomend? i have not edited my "postform" script but im still not seeing a result, or error with the session variable. if there is an easierand more efficent way, please advise? Cheers
February 22, 201115 yr what would you reccomend? i have not edited my "postform" script but im still not seeing a result, or error with the session variable. if there is an easierand more efficent way, please advise? Cheers What's your schema for your comments table?
February 22, 201115 yr Just pull the email out of the database like the rest of ur data within the while loop. while ($comments = mysqli_fetch_array($sql_get_results)) { $title = $comments['title']; $comment = $comments['comment']; $time = $comments['time']; $email = $comments['email']; $display_comments .= " <div class=\"comment_holder\"> <div class=\"comment_title\">".$title."</div> <div class=\"comment_email\">".$email."</div> <div class=\"comment_stamp\">".$time."</div> </div> <br /> <div class=\"comment_comment\">".$comment."</div> "; } and i don't think rallport was really being rude u see its alittle odd that u knew how to pull everything else from mysql, but did not know how to just do $comments['email'] to pull the email address, only my 2 sents though, hope that helps. and another thing that struck me as odd is ur selecting the email here $email_query = "SELECT email FROM users"; but ur using a session to get the email address Edited February 22, 201115 yr by webdesigner93
February 22, 201115 yr Author Just pull the email out of the database like the rest of ur data within the while loop. while ($comments = mysqli_fetch_array($sql_get_results)) { $title = $comments['title']; $comment = $comments['comment']; $time = $comments['time']; $email = $comments['email']; $display_comments .= " <div class=\"comment_holder\"> <div class=\"comment_title\">".$title."</div> <div class=\"comment_email\">".$email."</div> <div class=\"comment_stamp\">".$time."</div> </div> <br /> <div class=\"comment_comment\">".$comment."</div> "; } and i don't think rallport was really being rude u see its alittle odd that u knew how to pull everything else from mysql, but did not know how to just do $comments['email'] to pull the email address, only my 2 sents though, hope that helps. and another thing that struck me as odd is ur selecting the email here $email_query = "SELECT email FROM users"; but ur using a session to get the email address yeah i tried a selection of different things, i have already put that code in place, but in my script that actually posts the comment how would i get the session email address then place it into the comments table? Cheers for getting back to me!
February 22, 201115 yr Author yeah i tried a selection of different things, i have already put that code in place, but in my script that actually posts the comment how would i get the session email address then place it into the comments table? Cheers for getting back to me! this is my code now for pulling the data. <?php session_start(); //connect to database $mysqli = mysqli_connect("connection is here"); //get items $sql_get_query = "SELECT title, comment, time, email FROM blog_pettit"; $sql_get_results = mysqli_query($mysqli, $sql_get_query) or die(mysqli_error($mysqli)); $email = $_SESSION['email']; if (mysqli_num_rows($sql_get_results) < 1) { $display_comments = "<p><em>There are currently no comments in the database. </em></p>"; } else { while ($comments = mysqli_fetch_array($sql_get_results)) { $title = $comments['title']; $comment = $comments['comment']; $time = $comments['time']; $email = $comments['email']; $display_comments .= " <div class=\"comment_holder\"> <div class=\"comment_title\">".$title."</div> <div class=\"comment_email\">".$email."</div> <div class=\"comment_stamp\">".$time."</div> </div> <br /> <div class=\"comment_comment\">".$comment."</div> "; } } ?> <?php echo $display_comments; ?> Edited February 22, 201115 yr by craigpettit
February 22, 201115 yr this is my code now for pulling the data. <?php session_start(); //connect to database $mysqli = mysqli_connect("connection is here"); //get items $sql_get_query = "SELECT title, comment, time, email FROM blog_pettit"; $sql_get_results = mysqli_query($mysqli, $sql_get_query) or die(mysqli_error($mysqli)); $email = $_SESSION['email']; if (mysqli_num_rows($sql_get_results) < 1) { $display_comments = "<p><em>There are currently no comments in the database. </em></p>"; } else { while ($comments = mysqli_fetch_array($sql_get_results)) { $title = $comments['title']; $comment = $comments['comment']; $time = $comments['time']; $email = $comments['email']; $display_comments .= " <div class=\"comment_holder\"> <div class=\"comment_title\">".$title."</div> <div class=\"comment_email\">".$email."</div> <div class=\"comment_stamp\">".$time."</div> </div> <br /> <div class=\"comment_comment\">".$comment."</div> "; } } ?> <?php echo $display_comments; ?> If a user is submitting there comment, u should just have a field for the email address and get rid of that session stuff
February 22, 201115 yr Author If a user is submitting there comment, u should just have a field for the email address and get rid of that session stuff Yeah I thought of that but I don't want any more feilds in the post comment form, I just what it to place the logged in uers email address in the posted comment section
February 22, 201115 yr Author ok this is getting annoying, here are all my scripts related to this. The post form <form method="POST" action="index2.php"> <h3>Subject:</h3> <input type="text" id="title" name="title"/> <br /> <h3>Comment:</h3> <textarea type="text" id="comment" name="comment" cols="61" rows="4"/></textarea><br /> <input type="submit" id="submit_comment" name="submit" value="Post Comment" /> </form> post form php <?php session_start(); $mysqli = mysqli_connect("connection is here"); $email = $_SESSION['email']; if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } else { $sql_command = "INSERT INTO blog_pettit (title, comment, email) VALUES ('".$_SESSION['email']."', 'title', 'comment')"; $sql_result = mysqli_query($mysqli, $sql_command); if ($sql_result == TRUE) { echo "your post has been submitted"; } else { printf("Your comment could not be posted at this time. Please check and try again. %s\n", mysqli_error($mysqli)); } } ?> index2 - selects the records and redirects <?php $mysqli = mysqli_connect("connection is here"); $title = $_POST['title']; $comment = $_POST['comment']; $email = $_SESSION['email']; $sql_statement = "INSERT INTO blog_pettit (`title`, `email`, `comment`) VALUES ('".$_POST["title"]."', '".$_SESSION["email"]."', '".$_POST["comment"]."')"; $results = mysqli_query($mysqli, $sql_statement) or die (mysqli_error($mysqli)); if ($results == TRUE) { header( "refresh:3;url=index.php" ); echo "Your comment has been posted. You will be redirected in 3 seconds!"; } else { header( "refresh:3;url=index.php" ); echo "Could not create post! pleast try again! You will be redirected in 3 seconds!"; } ?> record select - gets the records and displays on idex.php <?php session_start(); //connect to database $mysqli = mysqli_connect("connection is here"); //get items $sql_get_query = "SELECT title, comment, time, email FROM blog_pettit"; $sql_get_results = mysqli_query($mysqli, $sql_get_query) or die(mysqli_error($mysqli)); $email = $_SESSION['email']; if (mysqli_num_rows($sql_get_results) < 1) { $display_comments = "<p><em>There are currently no comments in the database. </em></p>"; } else { while ($comments = mysqli_fetch_array($sql_get_results)) { $title = $comments['title']; $comment = $comments['comment']; $time = $comments['time']; $email = $_SESSION['email']; $display_comments .= " <div class=\"comment_holder\"> <div class=\"comment_title\">".$title."</div> <div class=\"comment_email\">".$email."</div> <div class=\"comment_stamp\">".$time."</div> </div> <br /> <div class=\"comment_comment\">".$comment."</div> "; } } ?> <?php echo $display_comments; ?> The script executes properly displays the comment title and timestamp, but no session email address and no errors. Please advise Cheeers
February 22, 201115 yr ok this is getting annoying, here are all my scripts related to this. The post form <form method="POST" action="index2.php"> <h3>Subject:</h3> <input type="text" id="title" name="title"/> <br /> <h3>Comment:</h3> <textarea type="text" id="comment" name="comment" cols="61" rows="4"/></textarea><br /> <input type="submit" id="submit_comment" name="submit" value="Post Comment" /> </form> post form php <?php session_start(); $mysqli = mysqli_connect("connection is here"); $email = $_SESSION['email']; if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } else { $sql_command = "INSERT INTO blog_pettit (title, comment, email) VALUES ('".$_SESSION['email']."', 'title', 'comment')"; $sql_result = mysqli_query($mysqli, $sql_command); if ($sql_result == TRUE) { echo "your post has been submitted"; } else { printf("Your comment could not be posted at this time. Please check and try again. %s\n", mysqli_error($mysqli)); } } ?> index2 - selects the records and redirects <?php $mysqli = mysqli_connect("connection is here"); $title = $_POST['title']; $comment = $_POST['comment']; $email = $_SESSION['email']; $sql_statement = "INSERT INTO blog_pettit (`title`, `email`, `comment`) VALUES ('".$_POST["title"]."', '".$_SESSION["email"]."', '".$_POST["comment"]."')"; $results = mysqli_query($mysqli, $sql_statement) or die (mysqli_error($mysqli)); if ($results == TRUE) { header( "refresh:3;url=index.php" ); echo "Your comment has been posted. You will be redirected in 3 seconds!"; } else { header( "refresh:3;url=index.php" ); echo "Could not create post! pleast try again! You will be redirected in 3 seconds!"; } ?> record select - gets the records and displays on idex.php <?php session_start(); //connect to database $mysqli = mysqli_connect("connection is here"); //get items $sql_get_query = "SELECT title, comment, time, email FROM blog_pettit"; $sql_get_results = mysqli_query($mysqli, $sql_get_query) or die(mysqli_error($mysqli)); $email = $_SESSION['email']; if (mysqli_num_rows($sql_get_results) < 1) { $display_comments = "<p><em>There are currently no comments in the database. </em></p>"; } else { while ($comments = mysqli_fetch_array($sql_get_results)) { $title = $comments['title']; $comment = $comments['comment']; $time = $comments['time']; $email = $_SESSION['email']; $display_comments .= " <div class=\"comment_holder\"> <div class=\"comment_title\">".$title."</div> <div class=\"comment_email\">".$email."</div> <div class=\"comment_stamp\">".$time."</div> </div> <br /> <div class=\"comment_comment\">".$comment."</div> "; } } ?> <?php echo $display_comments; ?> The script executes properly displays the comment title and timestamp, but no session email address and no errors. Please advise Cheeers ok ur not getting it clearly, get rid of the session email address, create a post variable, that relates to a field where the user enters there email then insert into mysql and display, U DO NOT USE SESSIONS TO STORE AN EMAIL ADDRESS USE MYSQL now idk how much more we can tell u, im not gonna sit here and make the script for u that u supposedly created, so don't sit there and act like we are being ignorant, ur being ignorant to the fact you don't use sessions to store an email. So stop getting annoyed and just fix the prob we told u clearly how to do it. If u do not like our replies then idk, don't post, and ps if theres not an email field use common since and create one Edited February 22, 201115 yr by webdesigner93
February 22, 201115 yr Author ok ur not getting it clearly, get rid of the session email address, create a post variable, that relates to a field where the user enters there email then insert into mysql and display, U DO NOT USE SESSIONS TO STORE AN EMAIL ADDRESS USE MYSQL now idk how much more we can tell u, im not gonna sit here and make the script for u that u supposedly created, so don't sit there and act like we are being ignorant, ur being ignorant to the fact you don't use sessions to store an email. So stop getting annoyed and just fix the prob we told u clearly how to do it. If u do not like our replies then idk, don't post no ur not getting it. i dont want a post variable i want it to grab the session. i know it is possible im asking for help on it. if you are not going to help. dont post. simple. stop wasting my time.
February 22, 201115 yr no ur not getting it. i dont want a post variable i want it to grab the session. i know it is possible im asking for help on it. if you are not going to help. dont post. simple. stop wasting my time. No simple use ur brain and know sessions are not used for that, and i won't help u so u can later down the road whine like a little baby and say ohh oops somethings wrong here so grow up and read ur standards u little prick. so after saying that stop waisting people's time who have been programming since u finally figured out how to change the color of a web page Edited February 22, 201115 yr by webdesigner93
February 22, 201115 yr and btw adding onto that u gotta set the session with data before u can use it, so pull alittle brain matter out of ur head will ya enough said
February 22, 201115 yr Author No simple use ur brain and know sessions are not used for that, and i won't help u so u can later down the road whine like a little baby and say ohh oops somethings wrong here so grow up and read ur standards u little prick. so after saying that stop waisting people's time who have been programming since u finally figured out how to change the color of a web page right. let me spell this out for you cus you clearly are not reading what is being said. why would a user who is already logged in need to write his/her email when i can clearly get it from the session. Edited February 23, 201115 yr by Wickham
February 22, 201115 yr right. let me spell this out for you cus you clearly are not reading what is being said. why would a user who is already logged in need to write his/her email when i can clearly get it from the session. Is it even in the session? Try print_r($_SESSION) and see what's there. Edited February 23, 201115 yr by Wickham
February 22, 201115 yr right. let me spell this out for you cus you clearly are not reading what is being said. why would a user who is already logged in need to write his/her email when i can clearly get it from the session. lol u pull the email from mysql of the logged in user duh, u don't have the mind set to be a coder keep with designing Edited February 23, 201115 yr by Wickham
February 22, 201115 yr Author and btw adding onto that u gotta set the session with data before u can use it, so pull alittle brain matter out of ur head will ya enough said the session is set on every page i have just selected the code. dont speak first and ask questions later.
February 22, 201115 yr Would u like me to spell this out for u learn to program there is numerous things wrong with ur code Edited February 23, 201115 yr by Wickham
February 22, 201115 yr Author Would u like me to spell this out for u learn to program there is numerous things wrong with ur code well funnily enough, i am trying to learn to program. and there is nothing wrong with my code except for the session email. as it works. weird. i spelt it out for u. dont u look silly now. Edited February 22, 201115 yr by craigpettit
February 22, 201115 yr well funnily enough, i am trying to learn to program. and there is nothing wrong with my code except for the session email. as it works. weird. i spelt it out for u. dont u look silly now. put ERROR_REPORTING(E_ALL); above session_start and see all the notices u get, then ask me whos silly lol
February 22, 201115 yr Author put ERROR_REPORTING(E_ALL); above session_start and see all the notices u get, then ask me whos silly lol already got it in there.
February 22, 201115 yr already got it in there. sure sure then ur php is not set up right go look in the php log this is what ur code should output if error reporting is working right, and actually u dont have it on any of the code u have shown but back to ur prob, for anyone to find out the prob u should be showing where the session is set at not the code u have shown Edited February 22, 201115 yr by webdesigner93
February 22, 201115 yr Author sure sure then ur php is not set up right go look in the php log this is what ur code should output if error reporting is working right, and actually u dont have it on any of the code u have shown but back to ur prob, for anyone to find out the prob u should be showing where the session is set at not the code u have shown as stated before, i have only included the relevent code.
February 22, 201115 yr as stated before, i have only included the relevent code. and as i stated above, seeing where ur session is set is relevent
February 22, 201115 yr Nnnnnnnn. print_r($_SESSION) Post it here if needs be. What's actually in there?
February 22, 201115 yr wow you are rude. maybe the fact ive never worked with session variables before. maybe try and help rather than trying to look cocky and actually coming off plain insulting. idiot. Get used to it. Amongst all the pompous, self-satisfied, superior members on here, Rallport is probably one of the worst. Not exactly a friendly individual. Just ignore. Doesn't really give much help either, just criticises people really. I'm not here to serve you honey. If I see someone bs'ing I'll say so thanks What kind of response is that? Just help the guy, FFS. Self-satisfied ****. this is a forum for people to help each other. your evidence that i have "bs'd" is nothing. il tell you what, go find that script, tell me the location and then you wont look like such a fool. Good luck Correct. This is a place for people to HELP each other. Unfortunately Web designers and developers, and graphic designers in my experience can be extremely arrogant, self satisfied, pompous people (not all of them of course). They often have a "I know it and you don't and I ain't sharing" type attitude, probably stemming from insecurity. Rallport was with no doubt at the same rung of the ladder as you were at one point, probably copied scripts, and probably asked the same kind of questions. Rallport seems to have forgotten that, haven't you Rallport. webdesigner93 ain't much better either - ok so the guy ain't getting it, couldn't you have just pointed him towards some kind of tutorial rather than getting impatient with him? Sheesh - what is UP with you guys, never seen such self-satisfied arrogance in my life....... Edited February 22, 201115 yr by Beeble
February 22, 201115 yr I'm guessing his username might be his e-mail address. Using a session variable when returning comment is daft as every different user will only see their own e-mail in the comments section. You already have the email in the database and being selected in the query, what's wrong with just adding this field to what you're trying to do?
February 22, 201115 yr lol ur an idiot u pull the email from mysql of the logged in user duh, u don't have the mind set to be a coder keep with designing This is exactly the kind of comment I'm talking about. What these guys don't tell you is that everyone started somewhere, including them. "If you don't have the mind to become a coder", "ur an idot" - well from someone who uses childish way of spelling "You're" i.e. "ur" - you webdesigner93 suddenly sound like a child and an idiot. Get over yourself. craigpettit - stick with it, you'll get there, buddy. Edited February 22, 201115 yr by Beeble
February 22, 201115 yr Beeble = craigpetit For what it's worth, webdesigner93 and rallport are a couple of helpful dudes (dudettes?) and have both helped me at some point. Edited February 22, 201115 yr by chrissyg
February 22, 201115 yr Author Get used to it. Amongst all the pompous, self-satisfied, superior members on here, Rallport is probably one of the worst. Not exactly a friendly individual. Just ignore. Doesn't really give much help either, just criticises people really. What kind of response is that? Just help the guy, FFS. Self-satisfied ****. Correct. This is a place for people to HELP each other. Unfortunately Web designers and developers, and graphic designers in my experience can be extremely arrogant, self satisfied, pompous people (not all of them of course). They often have a "I know it and you don't and I ain't sharing" type attitude, probably stemming from insecurity. Rallport was with no doubt at the same rung of the ladder as you were at one point, probably copied scripts, and probably asked the same kind of questions. Rallport seems to have forgotten that, haven't you Rallport. webdesigner93 ain't much better either - ok so the guy ain't getting it, couldn't you have just pointed him towards some kind of tutorial rather than getting impatient with him? Sheesh - what is UP with you guys, never seen such self-satisfied arrogance in my life....... Thank you.!
February 22, 201115 yr Author This is exactly the kind of comment I'm talking about. What these guys don't tell you is that everyone started somewhere, including them. "If you don't have the mind to become a coder", "ur an idot" - well from someone who uses childish way of spelling "You're" i.e. "ur" - you webdesigner93 suddenly sound like a child and an idiot. Get over yourself. craigpettit - stick with it, you'll get there, buddy. Cheers!!!
February 22, 201115 yr Author Beeble = craigpetit For what it's worth, webdesigner93 and rallport are a couple of helpful dudes (dudettes?) and have both helped me at some point. webdesigner93 has helped me before, his approach today was unexpected and uncalled for.
February 22, 201115 yr Beeble = craigpetit For what it's worth, webdesigner93 and rallport are a couple of helpful dudes (dudettes?) and have both helped me at some point. I ain't Craigpettit, I'll tell ya that much. Not sure about webdesigner93 but Rallport - yeah sure if he can lower himself from his higher echelon for long enough and your request is not "beneath" him and he doens't "judge" you to be an idiot sure you may get a bit of help. But as far as helping beginners though - if he deems your request unworthy, he'll sure be rude to ya. Seems to think he has the right to do this.
February 22, 201115 yr Author I ain't Craigpettit, I'll tell ya that much. Not sure about webdesigner93 but Rallport - yeah sure if he can lower himself from his higher echelon for long enough and your request is not "beneath" him and he doens't "judge" you to be an idiot sure you may get a bit of help. But as far as helping beginners though - if he deems your request unworthy, he'll sure be rude to ya. Seems to think he has the right to do this. yeah how do i = you?
February 22, 201115 yr Nothing wrong with using SESSION to obtain the email address, just make sure when the dude logs in that you pull the email from the database upon logging in, then store it in the Session. The point is you have to actually have it in the session in the first place to be able to request it from the session. it goes : User logs in and is identified. At this point pull all the stuff you want to store in the session about them (Forename, email, etc.) from their row in the database table. Then you can call upon this session data on any page they go to without having to make any more database queries for it. For example : $_SESSION['email'] = $row['email'] - where $row is the row you pulled from the database table when his login was recognised. If you're having diffculty just look up sql requests, php sessions etc. and work your way through it slowly but surely.
February 22, 201115 yr Author Nothing wrong with using SESSION to obtain the email address, just make sure when the dude logs in that you pull the email from the database upon logging in, then store it in the Session. The point is you have to actually have it in the session in the first place to be able to request it from the session. it goes : User logs in and is identified. At this point pull all the stuff you want to store in the session about them (Forename, email, etc.) from their row in the database table. Then you can call upon this session data on any page they go to without having to make any more database queries for it. For example : $_SESSION['email'] = $row['email'] - where $row is the row you pulled from the database table when his login was recognised. If you're having diffculty just look up sql requests, php sessions etc. and work your way through it slowly but surely. so i call the session variable as stated in my script above. but in my login scripts i need to make sure the variable is available? Cheers for this.
February 22, 201115 yr This is exactly the kind of comment I'm talking about. What these guys don't tell you is that everyone started somewhere, including them. "If you don't have the mind to become a coder", "ur an idot" - well from someone who uses childish way of spelling "You're" i.e. "ur" - you webdesigner93 suddenly sound like a child and an idiot. Get over yourself. craigpettit - stick with it, you'll get there, buddy. i can spell however i wanna spell, if u don't like it tough , noobs just don't like to hear the hard truth
February 22, 201115 yr Nothing wrong with using SESSION to obtain the email address, just make sure when the dude logs in that you pull the email from the database upon logging in, then store it in the Session. The point is you have to actually have it in the session in the first place to be able to request it from the session. it goes : User logs in and is identified. At this point pull all the stuff you want to store in the session about them (Forename, email, etc.) from their row in the database table. Then you can call upon this session data on any page they go to without having to make any more database queries for it. For example : $_SESSION['email'] = $row['email'] - where $row is the row you pulled from the database table when his login was recognised. If you're having diffculty just look up sql requests, php sessions etc. and work your way through it slowly but surely. and actually theres a better way to do this, that is using the users id such as $sql = mysql_query("SELECT * FROM users WHERE id = '".$_SESSION['id']."'"); better yet u can create a function to pull this info function user_data($data){ $sql = mysql_query("SELECT * FROM users WHERE id = '".$_SESSION['id']."'"); $pull = mysql_fetch_array($sql) or die (mysql_error()); return $pull[$data]; } then u can use the function like so echo user_data('email'); //Or echo user_data('firstname'); ect.. not tested yet should work so see no need to store the users email lol, i dont know of any login system that does that better to store the users unique id, and pull data as needed using a user created function Edited February 22, 201115 yr by webdesigner93
Create an account or sign in to comment