0

i am trying to insert data to my PostgreSQL table, i am getting date as string from form, but i end up with this error

ERROR: malformed array literal: "" LINE 2: ...d','2018-07-27 00:00:00','2018-07-27 00:00:00','','',1,1,'',... ^ DETAIL: Array value must start with "{" or dimension information.

this is my query

insert into events (title,location,description,start_date,end_date,start_time,end_time,is_pet,is_child,untill,repeat) values ('test','asdasd','asdasd',2018-07-27 00:00:00,2018-07-27 00:00:00,'','',1,1,'','Does Not Repeat')

and my query is parsed like this

$query = "insert into events (title,location,description,start_date,end_date,start_time,end_time,is_pet,is_child,untill,repeat) values 
          ('$title','$location','$description',$start_date,$end_date,'$start_time','$end_time',$is_pet,$is_child,'$until','$repeat')";

$sql = pg_query($query);
if (!$sql) {
    echo pg_last_error($db);
} else {
    echo "Records created successfully\n";
}

My Schema as follows

CREATE TABLE public.events (
    id integer NOT NULL,
    title character varying(255),
    location character varying(255),
    description character varying(255),
    end_date timestamp without time zone,
    start_time character varying(20),
    end_time character varying(20)[],
    image character varying(500),
    is_pet boolean,
    is_child boolean,
    repeat character varying,
    created_at timestamp without time zone,
    updated_at timestamp without time zone,
    posted_by character varying(255),
    email character varying(255),
    untill date,
    start_date timestamp without time zone
);
2
  • In your table, end_time is an array. Presumably that's a mistake as it makes no sense. That's why PG is complaining, you're giving it something that is not an array and it's telling you an array must start with { Commented Jul 28, 2018 at 15:55
  • In your example query output, and sql string build... you appear to be missing the single quotes around the date strings. This may cause an issue. Try first to just add single quotes around $start_date and $end_date. However, a better solution would to do this with prepared statements, so that you don't have to worry about proper quoting and escaping of data in a sql query. Begin with pg_prepare. Commented Jul 28, 2018 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.