I’ve got postgres table with an auto-incremental column, and attempting to insert data from PHP with pg_insert returns a string key error. How would I go about modifying this for the different datatypes?
$username = $_SESSION['username']; $data_timestamp = array(round(microtime(true) * 1000)); $data_username = array($username); array_splice($data_to_store, 0, 0, $data_timestamp); array_splice($data_to_store, 3, 0, $data_username); var_dump($data_to_store); $last_id = pg_insert($getPgInstance, 'app.features', $data_to_store);
Currently returns the following error:
Warning: pg_insert(): Accepts only string key for values in /var/www/html/admin/add_table.php on line 24 insert failed: Array ( [0] => 1609941177697 [1] => username [2] => real_name [3] => staff_account [4] => dummydatareason [5] => accounttype )
A var_dump returns:
array(6) { [0]=> float(1609941177697) ["username"]=> string(4) "user" ["real_name"]=> string(4) "disp" [1]=> string(15) "dummydatareasontext" ["reason"]=> string(14) "sdfdsfpassssss" ["Type"]=> string(13) "accounttype" }
My Postgres table is made up of:
id integer NOT NULL DEFAULT nextval('app.features_id_seq'::regclass), "timestamp" bigint, username character varying(20) COLLATE pg_catalog."default", real_name character varying(20) COLLATE pg_catalog."default", staff_name character varying(100) COLLATE pg_catalog."default", reason character varying(500) COLLATE pg_catalog."default", type character varying(100) COLLATE pg_catalog."default", CONSTRAINT features_pkey PRIMARY KEY (id)
Advertisement
Answer
From the PHP manual:
pg_insert() inserts the values of assoc_array into the table specified by table_name.
That means you can’t use a numeric array, like you do over here: [0]=> float(1609941177697)
I wouldn’t use this function anyway, please check pg_query_params(), pg_query() and pg_prepare/pg_execute