Jump to content

Archived

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

simcoweb

Error message in syntax but not sure why

Recommended Posts

This is a simple script designed to log an IP address of the visitor as well as the date of the visit. Basically it's a default page that, if landed on' will log the IP address and date then redirect the visitor to an authorized page. Here's the code:

[code]<?php //this is your default page for your unprotected directories
$ip = $_SERVER["REMOTE_ADDR"];
include 'config.php'; //edit the location of your logging file or MySQL database in the config.php

//set the date
$v_date = date("l d F H:i:s");

print "<table width='500' align='center' border='1'><tr><td>";
print "<table width='100%'><tr><td bgcolor='#C8D4DF'>";
echo "<center><font face='Verdana'><h3>Unauthorized Access Warning Message</h3></font>";
print "</td></tr>";
print "<tr><td bgcolor='#FFF0C0'>";
print "<center><h3>You should not be here</h4><p>";
print "</td></tr>";
print "<tr><td bgcolor='#FFF0C0'>";
echo "<center><font size='2' face='Verdana'>Your IP address is ". $ip . " and has been logged</font><p>";

echo "$v_date<p>";

echo "<center><font size='1' face='Verdana'><strong>You will be redirected to an authorized page.</strong></font>";
print "</td></tr></table></td></tr></table>";

//open the MySQL database and write to the table
mysql_connect('localhost', 'username', 'password') or die('Database will not open');
mysql_select_db('test');

// Insert a row of information into the table "example"
mysql_query("INSERT INTO iplog
(ip, date) VALUES($ip, $v_date) ")
or die(mysql_error()); 

echo "Data logged successfully!<br />";

?>[/code]


When I try to execute this I get this error:

[quote]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 ' Sunday 16 July 14:39:48)' at line 2[/quote]

However, line 2 has nothing to do with the date. I'm using MySQL 5.0.21 with XAMP. Any ideas why this is happening? Or, perhaps what the proper syntax would be to insert the date? Thanks in advance for your help!

Share this post


Link to post
Share on other sites
The line 2 in the message is not the line 2 in your code, it means the line 2 of the query that caused the problem.

The problem is in these lines:
[code]<?php
mysql_query("INSERT INTO iplog
(ip, date) VALUES($ip, $v_date) ")
or die(mysql_error()); ?>[/code]

Strings in MySQL need to be enclosed in single quotes.

I would write this as:
[code]<?php
$q = "INSERT INTO iplog (ip, date) VALUES('$ip', '$v_date') "
$rs = mysql_query($q) or die("There was a problem with the query: $q<br>" . mysql_error());
}?>[/code]

This way, if an error is produced, you know which query is in error and you know what the query looks like.

Ken

Share this post


Link to post
Share on other sites
Dayum! You guys are fast! :)

Ok, the single quotes got rid of the immediate error. I did, however, adapt your code suggestion as well, kenrbnsn. Thanks for that.

I'll repost if there's any additional issues. Thanks again!

Share this post


Link to post
Share on other sites

×

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.