0

Sir, my table look like this

aa.png

I am using this query to insert data

         $query = "INSERT INTO history(user, login, logout, duration, user_ip, user_lo)
          VALUES ('".$muser."','".($date('Y-m-d H:i:s')."','".($date('Y-m-d H:i:s')."',1,'".$_SERVER['SERVER_ADDR']."','myplace')";

But it says:
Parse error: syntax error, unexpected ';' in C:\wamp\www......

I am unable to know what is wrong in my query, please help

2
Contributors
1
Reply
17
Views
1 Year
Discussion Span
Last Post by cereal
0

Hi,

look I will not fix that query as it would be wrong in any case. Use prepared statements.

Example with PDO:

$query = "INSERT INTO `history` (`user`, `login`, `logout`, `duration`, `user_ip`, `user_lo`) VALUES (:user, :date, :date, 1, :ip, 'myplace')";

Then use bindParam():

$stmt = $conn->prepare($query);
$stmt->bindParam(':user', $muser);
$stmt->bindParam(':ip', $_SERVER['REMOTE_ADDR']);
$stmt->bindParam(':date', date('Y-m-d H:i:s'));

if($stmt->execute() === TRUE) echo 'success';
else echo 'fail';

Instead of the :date placeholder, if you're using MySQL, you could simply use the NOW() function. Docs: http://php.net/manual/en/pdostatement.bindparam.php

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.