Reputation: 3938
I am trying to insert some data into a table in a PostgreSQL database. I use prepared statements for this purpose like:
$db = new PDO('pgsql:dbname=wind;user=postgres;password=1249;host=localhost;port=5432');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
for ($x = 0; $x < sizeof($arrayAddress); $x++) {
$stmt = $db->prepare("INSERT INTO geocoding_test (address,googlemaps) VALUES (address=:address, sucUn=:sucUn ) ");
$stmt->execute(array(address => $arrayAddress[$x], sucUn => $arraySucUnsuc[$x] ));
}
...where arrayAddress
is an array containing all the addresses and arraySucUnsuc
is an array containing some string.
When I execute this I get the following error:
Fatal error: Uncaught exception 'PDOException' with message
SQLSTATE[42703]: Undefined column: 7 ERROR: column 'address' does not exist
LINE 1: ... INTO geocoding_test (address,googlemaps) VALUES (address=$1... ^
HINT: There is a column named 'address' in table 'geocoding_test', but it cannot be
referenced from this part of the query.' in C:\MAMP\htdocs\Wind\predictVdslEligibilityWSLog-20150614\myWebCrawler.php:98
Stack trace:
#0 C:\MAMP\htdocs\Wind\predictVdslEligibilityWSLog-20150614\myWebCrawler.php(98): PDOStatement->execute(Array)
#1 {main} thrown in C:\MAMP\htdocs\Wind\predictVdslEligibilityWSLog-20150614\myWebCrawler.php on line98
What am I doing wrong?
Also this is the definition of my table in the database:
CREATE TABLE geocoding_test
(
id serial NOT NULL,
address character varying(600),
googlemaps character varying(50)
)
Upvotes: 1
Views: 723
Reputation: 360572
Insert syntax is:
INSERT INTO tablename (fieldname1, ...) VALUES (value1, ...)
You have
... VALUES (address=:address, ...
where you're trying to COMPARE (=
) an existing value address
against the parameter you're passing. Since this is an INSERT
query, that address1
value doesn't exist yet: you can't read from what doesn't exist.
The query should be just
.... VALUES(:address, :sucUn)
Upvotes: 4