0

I have a PostgreSQL query to insert data into my table but for some reason, It's not inserting the data into the table

When I var_dump the execute statement if returning false.

foreach($information as $data){
     $name = $data['name'];
     $id = $data['id'];
    $time = $data['time'];

    $query =  "INSERT INTO students(name,id,time)"
               ."VALUES('$name','$id','time')";
                  //prepare and execute
               $stmt = $psql->prepare($query);
               $result = $stmt->execute();

               var_dump($result);


}

1 Answer 1

2

Most probably '$name' is not being understood in the query. Also, you should check for the existence of those variables

Try doing something like this

   if (isset($name, $id, $time)) {
    $query = 
    'DO$$
    BEGIN
        IF NOT EXISTS(select 1 from students where name = \'$name\')
            THEN
            INSERT INTO student("name", "id", "time")
            VALUES (\'$name\', \'$id\', \'$time\')
        END IF;
    END;
    $$;'
}
Sign up to request clarification or add additional context in comments.

2 Comments

After writing you above mention code how would I execute it?
Just put it inside a variable $query see my updated answer

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.