-3

I am trying to code a little log thing for my Home automations script but then I got this error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2

My SQL is:

INSERT INTO
    logsa (timeb, msg, actionb)
VALUES 
    ('12-05-2018 02:29:38pm',
    'Succesfully send a trigger to https://maker.ifttt.com/trigger/test/with/key/xxxxxxxxxxxxxxxxxxxxxxxx With name test',
    'https://maker.ifttt.com/trigger/test/with/key/xxxxxxxxxxxxxxxxxxxxxxxx'

and my code is:

    $logmsg = ("Succesfully send a trigger to " . $row["actiona"] . " With name " . $row["namea"]);

    date_default_timezone_set("Europe/Stockholm");

    $date = date("d-m-Y");
    $time = date("h:i:sa");
    $fulldate = ($date . " " . $time);

    $actiona = $row["actiona"];
    $sql = "INSERT INTO logsa (timeb, msg, actionb)
    VALUES ('$fulldate', '$logmsg', '$actiona'";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
7

1 Answer 1

0

Change the timeb data value $fulldate to use a valid date time format like this

$fulldate = date('Y-m-d H:i:s');

MySQL and mariaDB expect DATETIME columns to be stored in a very specific format and yours was invalid.

Also you should be using prepared and parametrised queries [link] to avoid SQL Injection attacks like this

$logmsg = ("Succesfully send a trigger to " . 
            $row["actiona"] . 
            " With name " . 
            $row["namea"]);

date_default_timezone_set("Europe/Stockholm");

$fulldate = date('Y-m-d H:i:s');
$actiona = $row["actiona"];

$sql = "INSERT INTO logsa (timeb, msg, actionb) VALUES (?,?,?)";
$stmt = $conn->prepare($sql);

$stmt->bind_param('sss', $fulldate, $logmsg, $actiona );
$result = $stmt->execute();
if ($result) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
1
  • No problem RiggsFolly. But thanks guys for all your help. Commented May 12, 2018 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.