• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5110
  • Last Modified:

PHP Insert Variables into database

I am trying to insert _SERVER date into a database. It is creating the visitors.html log page fine but I am getting the error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near (_SERVER data) '18-10-2007 10:41:45,,207.138.184.2,Mozilla/4.0 (compatible; MSIE 7.0; Windows NT' at line 1"
When I put static values into the code, the INSERT works perfectly.

Here is the code:

<?php
    define("DATE_FORMAT","d-m-Y H:i:s");
    define("LOG_FILE","visitors.html");

    $logfileHeader='
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Visitors log</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <table cellpadding="0" cellspacing="1">
    <tr><th>DATE</th><th>IP</th><th>HOSTNAME</th><th>BROWSER</th><th>URI</th><th>REFERRER</th></tr>'."\n";

    $userAgent = (isset($_SERVER['HTTP_USER_AGENT']) && ($_SERVER['HTTP_USER_AGENT'] != "")) ? $_SERVER['HTTP_USER_AGENT'] : "Unknown";
    $userIp    = (isset($_SERVER['REMOTE_ADDR'])     && ($_SERVER['REMOTE_ADDR'] != ""))     ? $_SERVER['REMOTE_ADDR']     : "Unknown";
    $refferer  = (isset($_SERVER['HTTP_REFERER'])    && ($_SERVER['HTTP_REFERER'] != ""))    ? $_SERVER['HTTP_REFERER']    : "Unknown";
    $uri       = (isset($_SERVER['REQUEST_URI'])     && ($_SERVER['REQUEST_URI'] != ""))     ? $_SERVER['REQUEST_URI']     : "Unknown";

    $hostName   = gethostbyaddr($userIp);
    $actualTime = date(DATE_FORMAT);

    $logEntry = "     <tr><td>$actualTime</td><td>$userIp</td><td>$hostName</td><td>$userAgent</td><td>$uri</td><td>$refferer</td></tr>\n";

    if (!file_exists(LOG_FILE)) {
        $logFile = fopen(LOG_FILE,"w");
        fwrite($logFile, $logfileHeader);
    }
    else {
        $logFile = fopen(LOG_FILE,"a");
    }

    fwrite($logFile,$logEntry);
    fclose($logFile);
?>

<?php
$dbuser="username";
$dbpass="password";
$dbname="database";  //the name of the database
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
    or die("Connection Failure to Database");
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found. " . $dbuser);

// insert data into table
$query = mysql_query("INSERT INTO zptrack (date_time, ip, hostname, browser, uri, referrer) values ($actualTime, $userIP, $hostName, $userAgent, $uri, $refferer)") or die(mysql_error());

//$query = mysql_query("INSERT INTO zptrack (clinicid, date_time, ip, hostname, browser, uri, referrer) values (                                           1,'1/1/1900','192.168.0.1','192.168.0.10','MSIE','something.php','index.php')") or die(mysql_error());

?>


0
terrysv1
Asked:
terrysv1
1 Solution
 
steelseth12Commented:
You must put your values in quotes

$query = mysql_query("INSERT INTO zptrack (date_time, ip, hostname, browser, uri, referrer) values ('$actualTime', '$userIP', '$hostName', '$userAgent', '$uri', '$refferer')") or die(mysql_error());

You could also use the mysql NOW() function to get the current date

$query = mysql_query("INSERT INTO zptrack (date_time, ip, hostname, browser, uri, referrer) values (NOW(), '$userIP', '$hostName', '$userAgent', '$uri', '$refferer')") or die(mysql_error());

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now
0
 
MikkkCommented:
You should do:
define("DATE_FORMAT","Y-m-d H:i:s");

and then:

$query = mysql_query("INSERT INTO zptrack (date_time, ip, hostname, browser, uri, referrer) values ('$actualTime', '$userIP', '$hostName', '$userAgent', '$uri', '$refferer')") or die(mysql_error());
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now