-1

i want to INSERT into db through external file log.php. id , date set in mysql DATETIME, and two strings. My function call is zapis_log($surename);. I'm getting error at prepare, and while executing.

log.php

<?php
require 'connect_test.php';
function zapis_log($who) {
$action = "edit";
    $log = $conn->prepare("INSERT INTO log (id,date,action,who) VALUES(NULL,NOW(),'$action','$who')");
    $insert = $log->execute(); 
}
?>

Thank you guys.

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
articos
  • 11
  • 2

1 Answers1

1
  1. date is a keyword. Use backtick to enclose date column.
  2. Not using bind_param?

UPDATED CODE

<?php
require 'connect_test.php';
function zapis_log($who) {
  $action = "edit";
  $current_date = date("Y-m-d h:i:s");

  $log = $conn->prepare("INSERT INTO log (`date`,action,who) VALUES(?,?,?)");
  $log->bind_param("sss", $current_date, $action, $who);
  $insert = $log->execute(); 
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • Nana, `date` is a keyword, not a reserved word https://dev.mysql.com/doc/refman/5.7/en/keywords.html - There is no `(R)` beside it. `ACTION` is also a keyword. Therefore backticking it won't fix the issue; something else is causing their code to fail and they need to check for (the real) error(s). Personally, I think it's a variable scope issue; look, they're using a custom function ;-) Plus, we also don't know which MySQL API they used to connect with. We also don't know how they're calling that `zapis_log()` function. And no idea where those variables are coming from and if they have values. – Funk Forty Niner Aug 03 '16 at 11:26