Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:42:17+00:00 2026-06-01T17:42:17+00:00

Hello I am making a class for doing multiple insert in pdo. It is

  • 0

Hello I am making a class for doing multiple insert in pdo.

It is something like this

INSERT INTO $table (key1,key2,key3,etc) VALUE (value1,value2,value3,etc), (value1,value2,value3,etc), (value1,value2,value3,etc)

After searching I found out that I have to build something like

INSERT INTO $table (key1,key2,key3,etc) VALUE (:key1,:key2,:key3,etc), (:key1,:key2,:key3,etc), (:key1,:key2,:key3,etc)

then execute with this $this->execute($data);
where $data is

 0 => 
    array
      'key1' => 'value1'
      'key2' => 'value2'
      'key3' => 'value3'
 1 => 
    array
      'key1' => 'value1'
      'key2' => 'value2'
      'key3' => 'value3'

 etc

the problem is i still get an error Array to string conversion on $insert->execute($data); how can i fix that?

here’s a snippet of what I’m making.

public function multipleInsert($table, $data = array()) 
{

    # INSERT (name) VALUE (value),(value)
    if (count($data) > 1) 
    {
        $fieldnames = array_keys($data[0]);
        $count_inserts = count(array_values($data));
        $count_values = count(array_values($data[0]));

        # array(????) untill x from first data
        for($i = 0; $i < $count_values; $i++)
        {
            $placeholder[] = '?';
        }

        # array((????),(????),(????)) for query
        for ($i=0; $i < $count_inserts; $i++) 
        { 
            $placeholders[] = '('. implode(',',$placeholder) . ')';
        }

        $query  = 'INSERT INTO '. $table;
        $query .= '(`'. implode('`, `', $fieldnames) .'`)';
        $query .= ' VALUES '. implode(', ', $placeholders);

        $insert = $this->start->prepare($query);

        $i = 1;
        foreach($data as $item) 
        {
            foreach ($item as $key => $value) 
            {
               $insert->bindParam($i++, $item[$key]);
            }
        }

        echo $query;
        $insert->execute();

        $return['status'] = true;
        $return['lastid'] = $this->start->lastInsertId();

        return $return;
    } 
    else 
    {
        die('$data is less then two array, use single insert instead.');
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:42:18+00:00Added an answer on June 1, 2026 at 5:42 pm

    An easy way for this avoiding the complications would be something like this

    $stmt = $pdo->prepare('INSERT INTO foo VALUES(:a, :b, :c)');
    foreach($data as $item)
    {
        $stmt->bindValue(':a', $item[0]);
        $stmt->bindValue(':b', $item[1]);
        $stmt->bindValue(':c', $item[2]);
        $stmt->execute();
    }
    

    However, this executes the statement multiple times. So, it is better if we create a long single query in order to do this.

    Here is an example of how we can do this.

    $query = "INSERT INTO foo (key1, key2) VALUES "; //Prequery
    $qPart = array_fill(0, count($data), "(?, ?)");
    $query .=  implode(",",$qPart);
    $stmt = $dbh -> prepare($query); 
    $i = 1;
    foreach($data as $item) { //bind the values one by one
       $stmt->bindValue($i++, $item['key1']);
       $stmt->bindValue($i++, $item['key2']);
    }
    $stmt -> execute(); //execute
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am having a hard time making this UI element look the way
Hello Android programmers, I am making a currency application and to do this, I
Hello can anybody solve this please I'm creating the object in the action class
I'm making my own string class and I'd like to ensure that CString a
I have a base class like this: class base(object): name = def __init__(self,name): self.user
Hello i am making an static library for distribution. I am distributing the .h
hello I am making web page searching program with windows batch file before searching
I'm experimenting with internationalization by making a Hello World program that uses properties files
I have a problem making an inner class that extends from JPanel to draw
I have this code: #include <iostream> using namespace std; class FooA { public: virtual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.