Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
pdo prepared statements exit with invalid parameter number

I have the following query:

$sql="INSERT INTO form_6 SET 
      Project-name=:Project-name,
      Project-manager-gruppo-CDT=:Project-manager-gruppo-CDT,
      Short-description=:Short-description,
      Status=:Status,
      Dependency-with-BB-Pj=:Dependency-with-BB-Pj,
      Critical-issues=:Critical-issues"

and the following array of data to be inserted:

Array ( 
    [:Project-name] => test 
    [:Project-manager-gruppo-CDT] => jack 
    [:Short-description] => simple project 
    [:Status] => on going 
    [:Dependency-with-BB-Pj] => yes 
    [:Critical-issues] => problems trying to insert data
)

and this is the code that I am using to run the query:

try{
    $stmt = $pdo->prepare($sql);
    $stmt->execute($values_array);
}
catch(PDOException $Exception){
    $message=$Exception->getMessage();
    $status=500;
    //ho avuto un problema e mi fermo
    die(json_encode(array('status'=>$status,'message' => $message)));
}

I really am not able to see why this terminates with the following exception:

Invalid parameter number: parameter was not defined

usually this comes from typos between the query and the array or using the same placeholder two times. But typos are excluded since I build the query and the array together using a foreach:

$values_array=array();
$sql = "INSERT INTO $tabella SET ";
foreach ($_POST as $key=>$value){
    $sql .= $key.'=:'.$key.',';
    $values_array[":$key"]=$value;
}
$sql=rtrim($sql,',');
echo $sql;  //this echoes the query at the beginning of the question
print_r($values_array);  //this echoes the array at the beginning of the question

What am I missing?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can't use - in parameter names. When you write :Project-name it's equivalent to :Profile - name, so it's expecting a parameter named :Profile, and then trying to subtract the column name from that.

Replace the - with _ in the placeholder.

Also, if a column name contains -, you need to put the name in backticks. See When to use single quotes, double quotes, and backticks in MySQL

$values_array=array();
$sql = "INSERT INTO $tabella SET ";
foreach ($_POST as $key=>$value){
    $placeholder = str_replace('-', '_', $key);
    $sql .= "`$key` = :$placeholder,";
    $values_array[":$placeholder"]=$value;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!