1

I am trying to build an SQL query that will insert the check-in time for a child at a fictional daycare facility. Here is a condensed version of my query code:

$childFirstName = $_POST['childFirstName'];
$childLastName = $_POST['childLastName'];

$now = new DateTime();
$nowDate = $now->format('m-d-Y');
$nowTime = $now->format('h:i');


$sql_childID = "SELECT id FROM child
                WHERE firstName = '$childFirstName'
                AND lastName = '$childLastName'";
$result = $pdo->query($sql_childID);
$row = $result->fetch();


$sql = "INSERT INTO checkinout(date, in, child_id) VALUES(?,?,?)";
    $statement = $pdo->prepare($sql);
    $statement->bindValue(1, $nowDate);
    $statement->bindValue(2, $nowTime);
    $statement->bindValue(3, $row['id']);
    $statement->execute();

The checkinout table uses VARCHAR datatypes for the date and in columns. Originally they were set to use DATETIME, but I received the same errors.

Right now I get the following errors returned...

enter image description here

You can see from the error messages that my values are getting passed in the way I want them to, but I don't understand where my syntax error would be.

1 Answer 1

3

Enclose your field names with backticks. Two of them are reserved words (date and in):

$sql = "INSERT INTO checkinout(`date`, `in`, `child_id`) VALUES(?,?,?)";

https://dev.mysql.com/doc/refman/5.5/en/keywords.html

Sign up to request clarification or add additional context in comments.

1 Comment

UPDATE....I didn't realize a backtick was different from a single quote. Makes me wonder how many other times I might have missed that. Thank you for your help and the link. This solved my problem.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.