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

  • Home
  • SEARCH
  • 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 5976845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:17:10+00:00 2026-05-22T21:17:10+00:00

I have a function to do a simple insert, but am trying to make

  • 0

I have a function to do a simple insert, but am trying to make the method more robust by passing an array.
And this is the array I pass into it:

        $form_data = array(
        "sort_order"=>$_POST['sort_order'],
        "name"=>$_POST['page_name'],
        "text"=>$_POST['page_text'],
        "image"=>$_POST['page_image'],
        "meta_desc"=>$_POST['meta_desc'],
        "meta_kw"=>$_POST['meta_kw'],
        "meta_author"=>$_POST['meta_author'],
        "image_thumb"=>"NULL",
    );

Here is the function code:

public function insert_data($array){
        $keys = array();
        $values = array();
        
        foreach($array as $k => $v){
            $keys[] = $k;
            if(!empty($v)){
                $values[] = $v;
            } else {
                $values[] = "NULL";
            }
        }

        $stmt = self::$mysqli->stmt_init();
        $query = "INSERT INTO `".DB_TABLE_PAGES."` (".implode(",",$keys).") VALUES (?,?,?,?,?,?,?,?)";
        
        
        $stmt->prepare($query);
        $stmt->bind_param('ssssssss',implode(",",$values));

        //$stmt->execute();
    }

But I get this error:

Number of elements in type definition string doesn’t match number of bind variables.

I know what the problem is, but I don’t understand how I can achieve it.

  • 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-05-22T21:17:10+00:00Added an answer on May 22, 2026 at 9:17 pm

    Try this:

    public function insert_data($array){
        $placeholders = array_fill(0, count($array), '?');
    
        $keys = $values = array();
        foreach($array as $k => $v) {
            $keys[] = $k;
            $values[] = !empty($v) ? $v : null;
        }
    
        $stmt = self::$mysqli->stmt_init();
        $query = 'INSERT INTO `'.DB_TABLE_PAGES.'` '.
                 '('.implode(',', $keys).') VALUES '.
                 '('.implode(',', $placeholders).')';
        $stmt->prepare($query);
    
        call_user_func_array(
            array($stmt, 'bind_param'), 
            array_merge(
                array(str_repeat('s', count($values))),
                $values
            )
        );
    
        $stmt->execute();
    }
    

    Or better yet, use PDO instead:

    public function insert_data($array){
        $placeholders = array_fill(0, count($array), '?');
    
        $keys = $values = array();
        foreach($array as $k => $v){
            $keys[] = $k;
            $values[] = !empty($v) ? $v : null;
        }
    
        // assuming the PDO instance is $pdo
        $query = 'INSERT INTO `'.DB_TABLE_PAGES.'` '.
                 '('.implode(',', $keys).') VALUES '.
                 '('.implode(',', $placeholders).')';
        $stmt = $pdo->prepare($query);
    
        $stmt->execute($values);
    }
    

    Note: I’ve used the null constant because the "NULL" string will be escaped as a string (not as a null value).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple function in which an array is declared with size depending
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I have a simple function which takes an array of characters as an argument,
I have the following simple function: private void EnableDisable941ScheduleBButton() { if (this._uosDepositorFrequency.Value != null)
I'm trying to run a simple insert query to a database. I have it
I have a simple form: function mymodule_test_form(&$form_state, $nid) { form['submit'] = array( '#type' =>
I have a simple function that I want to call in the code behind
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
I have a simple function to open an editor: open_an_editor() { nano $1 }
I have a simple function that returns an NSString after decoding it. I use

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.