0

i have a data array e.g. this:

$data = ['ver_weather' => $post["weathercondition"],
         'ver_flash'   => $post["flashintense"],
         'ver_earth'   => $post["earthrumble"]]

these data i use in my sql like this:

$sql = "INSERT INTO `database`.`table` (`weather`, `flash`, `earth`)
        VALUES (:ver_weather, :ver_flash, :ver_eart)";

$pdo->prepare($sql)->execute($data);

The result is something like:

INSERT INTO `database`.`table` (`weather`, 'flash', `earth')
VALUES ('1'weather, '1'flash, '1'earth)

So is pdo replacing parts of my keys with the value ?

what is wrong there ?

thanks for helping me out.

Shao Khan
  • 441
  • 6
  • 31

1 Answers1

1

edit: Execute does work with named bindings so you could just edit your $data array like this:

$data = [':ver_weather' => $post["weathercondition"],
         ':ver_flash'   => $post["flashintense"],
         ':ver_earth'   => $post["earthrumble"]]

Note the : at the beginning of each key

Original answer below...

I think the issue is that you're trying to bind by name and I don't think PDOStatement supports named bindings. I'd recommend trying the following:

$data = [$post["weathercondition"], $post["flashintense"], $post["earthrumble"]];
$sql = "INSERT INTO `database`.`table` (`weather`, `flash`, `earth`)
        VALUES (?, ?, ?)";

$pdo->prepare($sql)->execute($data);
James C
  • 14,047
  • 1
  • 34
  • 43
  • I'm fairly positive that you don't need the `:` in the key of the array. I'm not sure where OP's problem is coming from, but it isn't that. ([More Info](https://stackoverflow.com/questions/17386469/pdo-prepared-statement-what-are-colons-in-parameter-names-used-for)) – GrumpyCrouton Sep 07 '18 at 18:07