Conditional INSERT INTO statement in postgres

That specific command can be done like this: insert into LeadCustomer (Firstname, Surname, BillingAddress, email) select ‘John’, ‘Smith’, ‘6 Brewery close, Buxton, Norfolk’, ‘cmp.testing@example.com’ where not exists ( select 1 from leadcustomer where firstname=”John” and surname=”Smith” ); It will insert the result of the select statement, and the select will only return a row if … Read more

PHP MYSQL UPDATE if Exist or INSERT if not?

I believe you are looking for the following syntax: INSERT INTO <table> (field1, field2, field3, …) VALUES (‘value1’, ‘value2′,’value3′, …) ON DUPLICATE KEY UPDATE field1=’value1′, field2=’value2′, field3=’value3’, … Note: With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row … Read more

MySQL and PHP – insert NULL rather than empty string

To pass a NULL to MySQL, you do just that. INSERT INTO table (field,field2) VALUES (NULL,3) So, in your code, check if $intLat, $intLng are empty, if they are, use NULL instead of ‘$intLat’ or ‘$intLng’. $intLat = !empty($intLat) ? “‘$intLat'” : “NULL”; $intLng = !empty($intLng) ? “‘$intLng'” : “NULL”; $query = “INSERT INTO data … Read more

Insert data in 3 tables at a time using Postgres

Use data-modifying CTEs: WITH ins1 AS ( INSERT INTO sample(firstname, lastname) VALUES (‘fai55’, ‘shaggk’) — ON CONFLICT DO NOTHING — optional addition in Postgres 9.5+ RETURNING id AS sample_id ) , ins2 AS ( INSERT INTO sample1 (sample_id, adddetails) SELECT sample_id, ‘ss’ FROM ins1 RETURNING user_id ) INSERT INTO sample2 (user_id, value) SELECT user_id, ‘ss2’ … Read more

Why I am getting Cannot pass parameter 2 by reference error when I am using bindParam with a constant value?

You need to use bindValue, not bindParam bindParam takes a variable by reference, and doesn’t pull in a value at the time of calling bindParam. I found this in a comment on the PHP docs: bindValue(‘:param’, null, PDO::PARAM_INT); P.S. You may be tempted to do this bindValue(‘:param’, null, PDO::PARAM_NULL); but it did not work for … Read more