Jump to content

[SOLVED] please help me with the problem of inserting info into database


littlepeg

Recommended Posts

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.

                            ...}

(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.

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.  ???

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?

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? :'( :'(

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.