littlepeg Posted May 6, 2007 Share Posted May 6, 2007 Hi I am trying to realize a function that after a user logged in, he/she can send comments. Hence, I am trying to get the session()'s user_id information, and insert it into the database. But somehow, the user_id could not be retrieved. Would you please help me sort out this problem? Please help! :'( :'( Here are my codes: <?php //sendcomment.php ob_start(); session_name ('YourVisitID'); session_start(); // Start the session. // If no session value is present, redirect the user. if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) { // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } $url .= '/index.php'; // Add the page. header("Location: $url"); exit(); // Quit the script. } // Check if the form has been submitted. if (isset($_POST['submitted'])) { require_once ('snypdb.php'); // Connect to the db. $errors = array(); // Initialize error array. // Check for a user name. if (empty($_POST['topic'])) { $errors[] = 'Please choose your topic.'; } else { $topic = $_POST['topic']; } if (empty($_POST['comment'])) { $errors[] = 'Please write your ideas or recommendation.'; } else { $comment = $_POST['comment']; } if (empty($errors)) { // If everything's OK. // Register the user in the database. // Check for previous registration. $query = "SELECT user_id FROM users WHERE user_id='{$_SESSION['user_id']}'"; $result = mysql_query($query); if (mysql_num_rows($result) == 1) { // Make the query. $query = "INSERT INTO comments (user_id, topic,comment, comment_date) VALUES ('{$_SEESION['user_id']}', '$topic','$comment', NOW() )"; $result = mysql_query ($query); // Run the query. if ($result) { // If it ran OK. ...} Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/ Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 (Please dont Post Unnecessary codes post that much codes thats needed It also helps us to help and also proyect you from your script get hacked) ---------------------------------------------------------------------- Seeing the user_id could not be retrieved. Would you please help me sort out this problem? Please help! What do mean by "could not be retrieved". Try using print_r($_SESSIONS); and Post its output. Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246593 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 Sorry, neel_basu, but where should I put print_r($_SESSIONS);? (Sorry, new to php) Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246600 Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 Any where but after session_start(); Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246603 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 I put it like this: ob_start(); session_name ('YourVisitID'); session_start(); // Start the session. print_r($_SESSIONS); ... What happened when I insert the information into database, the values for user_id all "0" rather than the user's id. ??? Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246605 Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 Now post the Output of print_r($_SESSIONS); Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246608 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 Hi, but there is no output at all. It just display the comment form page. Do you mean what stored in the SESSIONS? Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246614 Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 Sorry typing mistake. Use print_r($_SESSION); Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246615 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 one letter makes a different. Here is the message: "Array ( [user_id] => 27 [first_name] => ting [agent] => f7ab13aacb2a32637ef1dc12d1b4a76b )" Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246617 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 What should I do next, please :'( Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246625 Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 Now use echo $_SESSIION['user_id']; Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246628 Share on other sites More sharing options...
john010117 Posted May 6, 2007 Share Posted May 6, 2007 No, its: $_SESSION['user_id']; Spelling counts. Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246630 Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 No, its: $_SESSION['user_id']; Spelling counts. Oh! Ya sorry. Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246633 Share on other sites More sharing options...
john010117 Posted May 6, 2007 Share Posted May 6, 2007 GAH! echo $_SESSION['user_id']; Use the above code. Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246634 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 Sorry, would you please tell me where should I put echo $_SESSION['user_id']; Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246635 Share on other sites More sharing options...
john010117 Posted May 6, 2007 Share Posted May 6, 2007 Replace: print_r($_SESSION); with: echo $_SESSION['user_id']; . Essentially the same place you've put the first code. Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246638 Share on other sites More sharing options...
neel_basu Posted May 6, 2007 Share Posted May 6, 2007 After print_r(); Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246639 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 Hi, john, when I replace print_r($_SESSION); with echo $_SESSION['user_id']; the output is "27" which is the user id, but why the value could not not insert into database? Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246655 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 Hi neel_basu, I tried to put echo $_SESSION['user_id']; after the print_r($_SESSION);, and it is the same message: "Array ( [user_id] => 27 [first_name] => ting [agent] => f7ab13aacb2a32637ef1dc12d1b4a76b )" Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246661 Share on other sites More sharing options...
john010117 Posted May 6, 2007 Share Posted May 6, 2007 I believe in this part of your code: // Register the user in the database. // Check for previous registration. $query = "SELECT user_id FROM users WHERE user_id='{$_SESSION['user_id']}'"; $result = mysql_query($query); if (mysql_num_rows($result) == 1) { // Make the query. $query = "INSERT INTO comments (user_id, topic,comment, comment_date) VALUES ('{$_SEESION['user_id']}', '$topic','$comment', NOW() )"; $result = mysql_query ($query); // Run the query. if ($result) { // If it ran OK. If the user already existed, then the script will actually register the person. Shouldn't it be the other way around? Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246666 Share on other sites More sharing options...
littlepeg Posted May 6, 2007 Author Share Posted May 6, 2007 Sorry, please ignore the comments: // Register the user in the database. (this is not the right comment, just some codes are the same from the registration form, so copied some and didnt change the comments yet ) What I thought was that, firstly, use these code to check the user id is the right user_id from the user table. $query = "SELECT user_id FROM users WHERE user_id='{$_SESSION['user_id']}'"; $result = mysql_query($query); if (mysql_num_rows($result) == 1) Then, insert this user_id into comments table by using the following codes: $query = "INSERT INTO comments (user_id, topic,comment, comment_date) VALUES ('{$_SEESION['user_id']}', '$topic','$comment', NOW() )"; $result = mysql_query ($query); // Run the query. Are these not right? If not, what should I do about it? :'( :'( Link to comment https://forums.phpfreaks.com/topic/50240-solved-please-help-me-with-the-problem-of-inserting-info-into-database/#findComment-246679 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.