0

I was following the examples given here on this thread

Why is insert not working and not outputting any error on postresql?

While I am trying to insert multiple values using this option inside the data base; I encountered this error "ERROR: syntax error at or near "Array" LINE 2: VALUES Array.." Could any assist please? Below is the code:

       …….

       "INSERT INTO people( mom, non, ooo)
      VALUES 
    ($1,   $2,  $3), 
   ($4,   $5,  $6),
   ($7,   $8,  $9),
   ($10, $11, $12)

  ON CONFLICT (mom) DO NOTHING";

   $rcon = pg_query_params($connection, $ins, 
   Array(
   [ $mom ,$non,  $kooo],
   [ $mom 1,$non1,  $kooo1],
   [ $mom 2,$non2,  $kooo2],
   [ $mom 3,$non3,  $kooo3]
       )
   );

   if(!$rcon){
   echo pg_last_error($connection);
       }else{
            echo "Record added to database</br>"; //success confirmation
      }


  This gives this error;  ERROR: bind message supplies 4 parameters, but prepared statement "" requires 12 
Vvictory
  • 45
  • 1
  • 11

2 Answers2

0

I don't see what arrays have to do with your question. Presumably, you want to insert multiple rows. That would be:

INSERT INTO people( mom, non, ooo)
VALUES 
    ($1,   $2,  $3), 
    ($4,   $5,  $6),
    ($7,   $8,  $9),
    ($10, $11, $12)
ON CONFLICT (mom) DO NOTHING
GMB
  • 216,147
  • 25
  • 84
  • 135
  • I did that and it showed this: ERROR: bind message supplies 4 parameters, but prepared statement "" requires 12 – Vvictory Jun 30 '20 at 21:51
  • @Vvictory To answer your problem here, the Array you are passing is an Array of Arrays. Get rid of the square brackets (`[` and `]`) inside of your `Array`, and it should work. But even if that does work for you, you are doing this wrong. – Mike Organek Jun 30 '20 at 22:01
  • @MikeOrganek could you suggest a better way please?.. because it keeps showing the bind message supplies error. – Vvictory Jun 30 '20 at 22:13
  • @Vvictory Please edit your question with your code updates and the error message. – Mike Organek Jun 30 '20 at 22:21
0

You're providing an array of arrays for your parameter binding. Try this instead:

    $rcon = pg_query_params($connection, $ins, 
        [
            $mom,  $non,   $kooo,
            $mom1, $non1,  $kooo1,
            $mom2, $non2,  $kooo2,
            $mom3, $non3,  $kooo3,
        ]
    );
Adam
  • 772
  • 3
  • 10