1

I am having success reading from 4D using PDO, but not writing to it.

When I try to Insert values into the database I get the following error:

SQLSTATE[HY000]: General error: 1248 Failed to execute statement.

Here is my code:

<?php
$dsn = 'dsn-info';
$user = 'user-info';
$pswd = 'password';

$db = new PDO($dsn, $user, $pswd);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Display PDO errors

$statement = $db->prepare("INSERT INTO order(dealer, customer) VALUES(:dealer, :customer)");

$statement->execute(array(
    "dealer" => "Test Dealer",
    "customer" => "Test Customer"
));
?>

Just trying to figure out whether there is something wrong with my INSERT statement or if something needs to be altered on the 4D side of things to give me writing permission.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Tyler
  • 119
  • 1
  • 11
  • `Order` is a reserved term (I think, I'm unfamiliar with "4D"). If this is a mysql sub-system I think it should be `INSERT INTO \`order\`` – chris85 Sep 27 '17 at 22:01
  • Don't you forget the double points ? --> array(":dealer" => "Test Dealer", ":customer" => "Test Customer") – YaatSuka Sep 27 '17 at 22:04
  • 1
    @YaatSuka PDO auto adds the colons on the binding if they are missing. – chris85 Sep 27 '17 at 22:05

1 Answers1

1

The query provided uses a SQL RESERVED WORD:

INSERT INTO order(dealer, customer) VALUES(:dealer, :customer)

This query uses ORDER as the table name, however ORDER is a SQL RESERVED WORD.

If you were to look at this table in the 4D Structure Editor's Table Inspector you would see something like this:
enter image description here

You cannot access this table via SQL, you should rename it.

Please see this tech tip: Tech Tip: Guidelines for SQL compatible field names

Tim Penner
  • 3,551
  • 21
  • 36
  • I ended up changing the table name to AppOrds to avoid using a reserved word, so now I have `"INSERT INTO AppOrds(dealer, customer) VALUES(:dealer, :customer)"`. Unfortunately I am still getting the same error. – Tyler Oct 04 '17 at 21:28