为什么我不能插入用户名但可以插入ID

时间:2012-11-26 15:21:06

标签: php mysql

下面我有代码并对其进行了大量编辑,除了以外所有内容都有效 数据库中的用户名字段没有更新,数据库设置正确,我也尝试使用session而不是vars。

请不要在此插入我想念的任何想法吗?

<?php
session_start();
?>
<?php 
//session_start();
$id = $_SESSION['user_id'];
$_SESSION['user_id']= $id;  
//$_SESSION['user_name'] = $full_name;
// $username = $_SESSION['user_name'];
//$username = $user_name;
$_SESSION['user_level'] = $user_level;
$_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);
$_SESSION['session_name'] = $user_name;
/****************************************************************************** 
 * Copyright (C) 2012 J~Net                                                   *
 *                                                                            *
 * This script gathers information from the user, including, but not limited  *
 * to their IP, and how long they viewed the page.  It then records it to a   *
 * MySQL database for our records / to monitor traffic to a site.             *
 * Usage : require("tracker.php");             Last modified : Nov 24th, 2012 *
 ******************************************************************************
 * This source and program come as is, WITHOUT ANY WARRANTY and/or WITHOUT    *
 * ANY IMPLIED WARRANTY.                                                      *
 *                                                                            *
 * Users of said software should realize that they cannot and will not hold   *
 * bombthebox.com reliable or responsible for any purpose WHAT SO EVER.       *
 * Please read all documentation and use said software responsibly.           *
 *                                                                            *
\******************************************************************************/

/* Add the static code from the include file */
require("include.php");

/* Obtain and format the current date & time */
$date_time = date("Y-m-d H:i:s");

/* SQL Statement to find out if the visitor is new or existing */
$SQL = "SELECT COUNT(*) AS previous_visitor FROM $db_tabl WHERE ip_address='$REMOTE_ADDR';";

/* Execute the SQL statement */
$results = mysql_db_query($db_name, "$SQL", $connection);

/* Error trapping on SQL */
if (!$results)
  echo("ERROR: " . mysql_error() . "\n$SQL\n");

/* Obtain the data from the record set */
while ($row = mysql_fetch_array($results))
 $previous_visitor = $row["previous_visitor"];

/* If the user is existing, then do this stuff */
if ($previous_visitor > 0)
  {
    /* SQL Statement to pull the information on the user's last visit */
    $SQL = "SELECT * FROM $db_tabl WHERE ip_address='$REMOTE_ADDR' ORDER BY id ASC;"; //AND page_viewed='$HTTP_REFERER' 

    /* Execute the SQL statement */
    $results = mysql_db_query($db_name, "$SQL", $connection);

    /* Error trapping on SQL */
    if (!$results)
      echo("ERROR: " . mysql_error() . "\n$SQL\n");

    /* Pull the data from the record set */
    while ($row = mysql_fetch_array($results))
      {
        $last_id = $row["id"];
        $last_time = $row["date_time"];
      }

    /* Split up the last timestamp for this user */
    $year   = substr($last_time, 0, 4);
    $month  = substr($last_time, 5, 2);
    $day    = substr($last_time, 8, 2);
    $hour   = substr($last_time, 11, 2);
    $minute = substr($last_time, 14, 2);
    $second = substr($last_time, 17, 2);

    /* Convert the user's last timestamp into seconds */
    $previous_time = mktime($hour, $minute, $second, $month, $day, $year);


// session_start();
$user = $_SESSION['user_name'];
// $user_name = $data['user_name'];
$id = $_SESSION['user_id'];
$_SESSION['user_name'] = $full_name;
$user_ip = $_SERVER['REMOTE_ADDR'];
$rq= $_SERVER['REQUEST_URI'];
$_SESSION['session_name'] = $user_name = $un;
$un = $user_name = $_SESSION['user_name'];

//$username = $_SESSION['logged_username'];
$_SESSION['user_name'] = $full_name;
// $user = $full_name;
$username = $_SESSION['user_name'] = $yourname;
$info["user_name"] =  $yourname;
$yourname = $_SESSION['user_name'] = $username;


    /* Split up the current timestamp for this user */
    $year   = substr($date_time, 0, 4);
    $month  = substr($date_time, 5, 2);
    $day    = substr($date_time, 8, 2);
    $hour   = substr($date_time, 11, 2);
    $minute = substr($date_time, 14, 2);
    $second = substr($date_time, 17, 2);

    /* Convert the user's current timestamp into seconds */
    $current_time = mktime($hour, $minute, $second, $month, $day, $year);

    /* Calculate how much time as elapsed since their last visit */
    $time_difference = $current_time - $previous_time;
  }

/* Update the last record with the elapsed time, assuming the value is less than an hour. */
if ($time_difference != "" && $time_difference < 3600)
  {
    /* SQL Statement to update an existing record */
    $SQL = "UPDATE $db_tabl SET time_spent=$time_difference WHERE id='$last_id';";

    /* Execute the SQL statement */
    $results = mysql_db_query($db_name, "$SQL", $connection);

    /* Error trapping on SQL */
    if (!$results)
      echo("ERROR: " . mysql_error() . "\n$SQL\n");
  }




/* SQL Statement to insert the data */
$SQL = "INSERT INTO $db_tabl (ip_address, page_viewed, date_time, username, user_id) VALUES ('$user_ip', '$rq', '$date_time','$yourname','$id') ;";

/* Execute the SQL statement */
$results = mysql_db_query($db_name, "$SQL", $connection);

/* Error trapping on SQL */
if (!$results)
  echo("ERROR: " . mysql_error() . "\n$SQL\n");

/* Close off the database */
mysql_close($connection);

?>

1 个答案:

答案 0 :(得分:3)

$username = $_SESSION['user_name'] = $yourname;
$info["user_name"] =  $yourname;
$yourname = $_SESSION['user_name'] = $username;

$ yourname在此之前未定义;所以你要给$ username分配一个不存在的变量的值,然后第3行从$ username中重新分配$ yourname,这是第1行的空....所以你会期待什么?

相关问题