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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:18:49+00:00 2026-06-10T08:18:49+00:00

I was doing a simple INSERT during my development, but I’m going back and

  • 0

I was doing a simple INSERT during my development, but I’m going back and putting in SQL protection and prepared statements. The error I receive is:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number:
number of bound variables does not match number of tokens

Here is the code that I’m using from help from others on SO.

// foreach to set up variables
foreach ($json as $text) {
    $uoid = mysql_real_escape_string($uoid);
    $filename = mysql_real_escape_string($uoid.".jpg");
    $filedate = mysql_real_escape_string($datetime);
    $imagedesc = mysql_real_escape_string($desc);

    // array of values
    $insert[] = array($uoid,$filename,$filedate,$imagedesc);
}

function placeholders($text, $count=0, $separator=","){
    $result = array();
    if($count > 0){
        for($x=0; $x<$count; $x++){
            $result[] = $text;
        }
    }
    return implode($separator, $result);
}

foreach($insert as $d){
    $question_marks[] = '('  . placeholders('?', sizeof($d)) . ')';
}        

$pdo = new PDO('mysql:dbname=photo_gallery;host=127.0.0.1', 'myuser', 'mypass');
$pdo->beginTransaction();
$sql = "INSERT INTO wp_gallery (" . implode(',', array_values($insert) ) . ") 
        VALUES " . implode(',', $question_marks);
// reading output
echo $sql;

$stmt = $pdo->prepare($sql);

try {
    $stmt->execute($insert[0]);
} catch (PDOException $e){
    echo $e->getMessage();
}

$pdo->commit();

When I look at the SQL output:

INSERT INTO wp_gallery (10219776,10219776.jpg,my image description,2012-08-01 15:36:29)
VALUES (?,?,?,?),(?,?,?,?),(?,?,?,?),(?,?,?,?)

Everything matches just by the look of it, but I’m still baffled as to how to fix this. Can someone point out what I’m doing wrong?

  • 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-10T08:18:51+00:00Added an answer on June 10, 2026 at 8:18 am

    You’re passing values as column names, and you’re not passing the correct parameter to execute;

    Something like this would be more correct;

    // Replace the $insert buildup. Array of values, you don't want an array of arrays here.
    $insert = array_merge($insert, array($uoid,$filename,$filedate,$imagedesc));
    
    ...
    
    $column_names = array("column1", "column2", "column3", "column4");
    
    $sql = "INSERT INTO wp_gallery (" . implode(',', $column_names ) . ") 
        VALUES " . implode(',', $question_marks);
    
    $stmt->execute($insert);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to create a simple SQL INSERT otherwise UPDATE statement but to be
Doing a simple Squeryl database lookup, but trying to exclude a value. I've tried:
I am doing a simple JSP program to connect with MySQL DB. But it's
I don't get it! I'm doing a simple insert in an access db. static
What am I doing wrong? It's a very simple statement but couldn't figure out
I'm stuck on something really simple but just can't see what I'm doing wrong.
I wrote a simple class: @Entity @Table(name = battle_log) @SQLInsert(sql = INSERT INTO battle_log
Ok, what I'm doing is (I think) rather simple. I have a LINQ-to-SQL data
I have a little proof-of-concept distributed transaction application that is doing a simple insert
I'm doing some SQL tuning these days and find one weird sql during the

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.