getting error in my php query
I am getting an error message in my php query.
Error being displayed: 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 '1294251744','127.0.0.1','/register')' at line 2 my code: <?php require_once("includes/database.php"); //Set timeout to 5 minutes $timeoutseconds = 300 ; //get the time $timestamp = time(); //Delete all users that are no online after the time out allowed $timeout = $timestamp - $timeoutseconds ; // stores users IP addresss $user_ip = $_SERVER['REMOTE_ADDR']; // Automatically collects the hostname or domain like example.com) $host = $_SERVER['HTTP_HOST']; $host_upper = strtoupper($host); $path = rtrim(dirname($_SERVER['PHP_SELF']), '/\'); //insert the values $sql = "INSERT INTO totalonline(timestamp, ip, file) VALUES (''$timestamp','$user_ip','$path')"; $result = mysql_query($sql, $conndb) or die(mysql_error()); //delete values when they leave mysql_query("DELETE FROM totalonline WHERE timestamp < $timeout"); //grab the results $sql = "SELECT DISTINCT ip FROM totalonline WHERE file='$path' "; $result = mysql_query($sql, $conndb) or die(mysql_error()); //number of rows = the number of people online $user = mysql_num_rows($result); //spit out the results if( $user == 1 ) { echo "$user User online"; } else { echo "$user User online"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Contact form</title> <meta name="description" content="" /> <meta name="keywords" content="" /> <meta http-equiv="imagetoolbar" content="no" /> <link href="style/core.css" rel="stylesheet" type="text/css" /> <script language="JavaScript" type="text/javascript" src="js/jquery-1.3.2.min.js"></script> </head> <body> <div id="wr"> <h1>Users</h1> </body> </html>also if I wanted to show the number of usrs in the html code, how would that be done using the same if( $user == 1 ) { echo "$user User online"; } else { echo "$user Users online"; } so that if only one user is online it says "1 user online" and if their were 2 or more users online it would give a message "2 users online" thanks
2 Answers
Change this:
//insert the values $sql = "INSERT INTO totalonline(timestamp, ip, file) VALUES (''$timestamp','$user_ip','$path')";to this: //insert the values $sql = "INSERT INTO totalonline(timestamp, ip, file) VALUES ('$timestamp','$user_ip','$path')";You had two single quotes instead of one. Also, near the end, you probably want to change this: if( $user == 1 ) { echo "$user User online"; } else { echo "$user User online"; }to this: if( $user == 1 ) { echo "$user User online"; } else { echo "$user User offline"; } Posted: Go 1 of 1 people found this answer helpful. Did you? Yes No
You have double \' in the values field;)
//insert the values $sql = \"INSERT INTO totalonline(timestamp, ip, file) VALUES (\'\'$timestamp\',\'$user_ip\',\'$path\')\";Right before $timestamp. It\'s better to do //insert the values $sql = \'INSERT INTO totalonline(timestamp, ip, file) VALUES (\'.$timestamp.\',\"\'.$user_ip.\"\',\"\'.$path.\'\")\';because that way you make sure the db understands ip and path are strings. Posted: MacOS 1 of 1 people found this answer helpful. Did you? Yes No |
© Advanced Web Core. All rights reserved