INSERT INTO SQL Heredoc correct syntax

Marcelo Noronha picture Marcelo Noronha · Oct 23, 2013 · Viewed 7.6k times · Source

I'm trying to use heredoc syntax with a INSERT INTO MySQL command, into a database.

The problem I'm facing is with the variable $tPerson_SQLinsert, which is a query.

Here's my code:

$tPerson_SQLinsert = <<<SQL
    INSERT INTO tPerson (Salutation, FirstName, LastName, Tel, companyID)
    VALUES ("$Salutation", "$FirstName", "$LastName", "$Tel", "$companyID")
    SQL;

Well, there´s probably something wrong with that syntax.

Because I´m getting this Parse error:

 Parse error: syntax error, unexpected end of file in F:\wamp\www\forms\personInsert.php on line 83

That is just that end tag of php ?>.

What would be the correct syntax for the heredoc?

Thanks.

Answer

aderuwe picture aderuwe · Oct 23, 2013

It will work when you format it like so:

$tPerson_SQLinsert = <<<SQL
    INSERT INTO tPerson (Salutation, FirstName, LastName, Tel, companyID)
    VALUES ("$Salutation", "$FirstName", "$LastName", "$Tel", "$companyID")
SQL;

I.e. the final delimiter should start in column 1.

See docs (in the warning block): https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc