0

I've been having trouble inserting data by calling it from a specific function. It's give me no errors but it just doesn't insert.

Here is the code

  function log_($moderator, $actions, $conn) 
{
    try 
    {
        $log = $conn->prepare("INSERT INTO moderation-history(moderator, action, date, ip_addr) VALUES (:Moderator, :Action, ".time().", ".$_SERVER['REMOTE_ADDR'].")");
        $log->bindParam(':Moderator', $moderator, PDO::PARAM_STR, 100);
        $log->bindParam(':Action', $actions, PDO::PARAM_STR, 100);
        $log->execute();
    }
        catch(PDOException $e) 
    {
         error_log("PDOException: " . $e->getMessage());

    }
}

and the code for it to execute once the Moderator changes the permissions

log_($user, "Altered ".$username." Permissions", $conn);

Executing this function should insert the data. As $user = Moderator, 2nd = Action, $conn = DB Connection.

Zesty
  • 273
  • 2
  • 6
  • 19
  • 1
    If it isn't clear from the linked duplicate, your issue is the unquoted string `$_SERVER['REMOTE_ADDR']` - that would need to be a single-quoted string. But really, you should just create a parameter for it as `:IpAddr` and `bindParam()` it. Now, if you see no errors reported, confirm that you have actually configured PDO to throw exceptions. By default, it errors silently after its constructor. Do you have `$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` before this action, after successfully instantiating `$conn`? – Michael Berkowski Jul 19 '15 at 23:34
  • 1
    Ah, another thing -- your table has a hyphen `moderation-history`. That would mean it needs to be quoted with backticks in MySQL as `\`moderation-history\``. I would advise changing the table's name to use an underscore instead as `moderation_history` if you are in any position to do so, because this can cause a lot of unnecessary headaches. – Michael Berkowski Jul 19 '15 at 23:38
  • @MichaelBerkowski I think your answer on the dupe is clear enough :) – Rizier123 Jul 19 '15 at 23:49
  • 1
    @Rizier123 For the string quoting sure, but the hyphenated table would be easy to miss (and I did miss it the first time!) Cheers! – Michael Berkowski Jul 19 '15 at 23:53
  • Well, I tried what you said and it doesn't work still for some reason. I'm executing this `log_($user, "Altered ".$username." Permissions", $conn);` – Zesty Jul 20 '15 at 00:46
  • Nevermind I found out the reason why it didn't insert... I made a typo with the table name. – Zesty Jul 20 '15 at 01:04

0 Answers0