0

I know questions like this exist all over, but I can't see what's wrong with my code. The DB works and I run an update query just fine earlier on in the code.

Query 1:

mysql_query("INSERT INTO `login_history` (`memberid`, `ip`, `host`, `location`, `status`, `date`) VALUES ('".$login."', '".$ip."', '".$details->hostname."', '".$loc."', 'success', NOW()");

Query 2:

mysql_query("INSERT INTO `login_history` (`memberid`, `ip`, `host`, `location`, `status`, `date`) VALUES ('".$login."', '".$ip."', '".$details->hostname."', '".$loc."', 'failure', NOW()");

Here is an echo of the string as requested:

INSERT INTO `login_history` (`memberid`, `ip`, `host`, `location`, `status`, `date`) VALUES ('AAL', '**.60.**.**', 'c-174-**-**-**.hsd1.**.comcast.net', 'Town, US', 'success', NOW()
11
  • 1
    You should echo out your SQL, I would be money that one of the variables is empty and therefore spoiling your insert statement. Commented May 16, 2014 at 0:58
  • 2
    Prepared statements! You won't regret it :) php.net/manual/en/mysqli.quickstart.prepared-statements.php Commented May 16, 2014 at 0:58
  • Would also recommend testing out your echo using phpmyadmin Commented May 16, 2014 at 1:00
  • What error do you get in your console? Commented May 16, 2014 at 1:01
  • 1
    you should post the actual query string here. If I had to guess I would say one of those variables has an apostraphe in it. That would terminate the value and cause a syntax error - prepared statements as jason said. Escaping values is pretty important Commented May 16, 2014 at 1:04

1 Answer 1

1

Looks like you are missing the final closing ). Also use prepared statements. Much cleaner and safer. Here is a quick example of what a Prepared Statement would look like (adapted from here) (you wold also need to make other changes to your PHP script to start using them)

$stmt = $mysqli->prepare("INSERT INTO `login_history` (`memberid`, `ip`, `host`, `location`, `status`, `date`) VALUES (?, ?, ?, ?, ?, NOW())")
$stmt->bind_param('sssss', $login, $ip, $details->hostname, $loc, 'failure');
$stmt->execute()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.