Question

I have the following php script that does not work with bindValue, but works if I put the params into the SQL string(i.e. if I use the commented line instead of the 6 lines later, it works).

<?php

$PDOconnessione = new PDO("mysql:host=127.0.0.1;dbname=eser_php-jquery","root","root");
/*$query = $PDOconnessione->prepare("INSERT INTO appartamento (Tipologia, Prezzo, CodCitta, Descrizione)
                                    VALUES ('" . $_GET["tipologia"] . "','" . $_GET["prezzo"] . "','" . $_GET["citta"] . "','" . $_GET["descrizione"] . "')" ); */


$query = $PDOconnessione->prepare("INSERT INTO appartamento (Tipologia, Prezzo, CodCitta, Descrizione)
                                    VALUES (:tipologia, :prezzo, :codcitta, :descrizione)");

$query->bindValues(':tipologia', $_GET["tipologia"]);
$query->bindValues(':prezzo', $_GET["prezzo"]);
$query->bindValues(':codcitta', $_GET["citta"]);
$query->bindValues(':descrizione', $_GET["descrizione"]);

$query->execute();

echo $PDOconnessione->lastInsertId();

$PDOconnessione = null;

?>

It's the first time that I try to use this method, so it's probably a simple error, but what is it?

Was it helpful?

Solution

The method name is bindValue, not bindValues. Had you enabled error reporting, you'd have found this out easily.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top