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 7542247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:06:58+00:00 2026-05-30T08:06:58+00:00

I have the following PHP PDO statement: $STH = $this->_db->prepare(INSERT INTO UserDetails (FirstName, LastName,

  • 0

I have the following PHP PDO statement:

$STH = $this->_db->prepare("INSERT INTO UserDetails (FirstName, LastName, 
            Address, City, County, PostCode, Phone, Mobile, Sex, DOB, 
            FundraisingAim, WeeksAim, LengthsAim, HearAboutID,
            MotivationID, WelcomePackID, ContactPrefID, TitleID) 
            VALUES
            (:firstName, :lastName, :address, :city, :county, :postCode, 
            :phone, :mobile, :sex, :DOB, :fundraisingAim, :weeksAim,
            :lengthsAim, :hearAbout, :motivation,
            :welcomePackPref, :contactPref, :title)");

$STH->execute($userData);

Where $userData is an associative array. I’ve double checked the names and I don’t understand why I’m getting the following error:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

What silly mistake have I made?

  • 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-30T08:07:00+00:00Added an answer on May 30, 2026 at 8:07 am

    Your $userData must have exactly the same placeholders bound by your statement, no more and no fewer. See PDOStatement::execute documentation, the part that says “You cannot bind more values than specified”.

    You need to prepare your argument to execute() to match your binds exactly. This is easy with array_intersect_key() if you arrange your arrays correctly. I usually wrap this in a function which will also take care of prefixing, like below:

    // Adds a prefix to a name for a named bind placeholder
    function prefix($name) {
        return ':'.$name;
    }
    
    // like 'prefix()', but for array keys
    function prefix_keys($assoc) {
        // prefix STRING keys
        // Numeric keys not included
        $newassoc = array();
        foreach ($assoc as $k=>$v) {
            if (is_string($k)) {
                $newassoc[prefix($k)] = $v;
            }
        }
        return $newassoc;
    }
    
    // given a map of datakeyname=>columnname, and a table name, returns an
    // sql insert string with named bind placeholder parameters.
    function makeInsertStmt($tablename, $namemap) {
        $binds = array_map('prefix', array_keys($namemap));
        return 'INSERT INTO '.$tablename.' ('.implode(',',$namemap).') VALUES ('
        .implode(',',$binds).')';
    }
    
    // returns an array formatted for an `execute()`
    function makeBindData($data, $namemap) {
        // $data assoc array, $namemap name->column mapping
        return prefix_keys(array_intersect_key($data, $namemap));
    }
    
    // example to demonstrate how these pieces fit together
    function RunTestInsert(PDO $pdo, $userData) {
        $tablename = 'UserDetails';
        // map "key in $userData" => "column name"
        // do not include ':' prefix in $userData
        $namemap = array(
          'firstName'       => "FirstName",
          'lastName'        => "LastName",
          'address'         => "Address",
          'city'            => "City",
          'county'          => "County",
          'postCode'        => "PostCode",
          'phone'           => "Phone",
          'mobile'          => "Mobile",
          'sex'             => "Sex",
          'DOB'             => "DOB",
          'fundraisingAim'  => "FundraisingAim",
          'weeksAim'        => "WeeksAim",
          'lengthsAim'      => "LengthsAim",
          'hearAbout'       => "HearAboutID",
          'motivation'      => "MotivationID",
          'welcomePackPref' => "WelcomePackID",
          'contactPref'     => "ContactPrefID",
          'title'           => "TitleID",
        );
        $sql = makeInsertStmt($tablename, $namemap);
        $binddata = makeBindData($userData, $namemap);
    
        $pstmt = $pdo->prepare($sql);
        $pstmt->execute($binddata);
    }
    

    The benefit of an abstraction like this is you don’t need to worry about the bind parameters themselves.

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

Sidebar

Related Questions

I have the following PHP code doing a very simple select into a table.
I'm having a strange problem with php PDO and mysql. I have the following
I have the following PHP code, which is meant to insert data in the
I'm using PHP PDO to run queries. I have the following function: protected function
I have following PHP code for inserting session data into a table called ds_session:
I have the following code: <?php $stmt = $db->prepare(select * from products where `price`=:price
I have following PHP code $val=<div id=user.$row['cid']. userid=.$row['cid']. class=innertxt><img src=images/images.jpg width=50 height=50><strong>.$uname.</strong><ul> <li>Email: .$row['cemail'].</li>
I have the following php code, which has been snipped for brevity. I am
I have the following PHP in my contact form. // Ensure a message was
I have the following PHP code which works out the possible combinations from a

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.