-2

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 ''stats' WHERE ip = '::1' && date = '28-11-2013'' at line 1.

 <?php

 require_once 'includes/config.php';

 $getStats = mysql_query("SELECT * FROM 'stats' WHERE `ip` = '" . $ip . "' && `date` = '" . $time ."'") or die(mysql_error());

 if( mysql_num_rows($getStats) == 0)
 {
    $select = mysql_query("INSERT INTO `stats` (`ip`,`data`,`hits`,`online`) VALUES ('" . $ip . "','" . $time . "', '1', '" . $timestamp ."')") or die(mysql_error()); 
     }
else
{
    $select = mysql_query("UPDATE `stats` SET `hits` = `hits`+1, `online` = '" . $timestamp . "' WHERE `ip` = '" . $ip . "' && `date` = '" . $time . "'");
    }
 ?>
Serafins
  • 1,237
  • 1
  • 17
  • 36
  • this problem will solved but another problem will come . This is new problem Unknown column 'data' in 'field list' –  Nov 28 '13 at 00:47

1 Answers1

3

MySQL quotes identifiers with a backtick like `stats`, and not like 'stats' - which is what causes the syntax error.

Either remove the useless identifier quote (stats is not reserved) or switch to proper identifier quotes.

The subsequent issue is that there is no data column, which is just as the error says. The correct column is probably date (which should be quoted, or perhaps renamed).

Also, dates should be supplied in the form of 'YYYY-MM-DD' (or variant). The fact that the query works with a locale-specific date makes me believe a proper DATE column is not used as it ought to be. Correcting the column type now will avoid issues in the future, especially with sorting.


In addition to the above corrections, also update the query to use mysqli/PDO and placeholders - this will tidy up the code, eliminate the use of deprecated functions, and prevent SQL injection (accidental or otherwise).

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • this problem will solved but another problem will come . This is new problem Unknown column 'data' in 'field list' –  Nov 28 '13 at 00:48
  • @user3036848 Also make sure the `\`date\`` column has the appropriate data-type: `DATE`. Otherwise you'll run into different issues, such as "incorrectly" ordered data. I've added an appropriate section to my answer. – user2864740 Nov 28 '13 at 01:00
  • https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-prn2/1412470_733414666686841_128887452_o.jpg , @user2864740 –  Nov 28 '13 at 01:36
  • @user3036848 Then put in the *correct value*. The code is putting in `timestamp`, which is probably a Unix time value. Note that the `hits` column already records 1..2..3..etc. – user2864740 Nov 28 '13 at 04:05