Click here to Skip to main content
16,018,006 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' on line 25
 

any one can help please?


function update($table,$data,$id){

 	global $conn;

  $resultstr = array();

  foreach ($data as $keys => $value) {

 	$resultstr[] = $keys."=:".$keys;


  }
 	
  $values = implode(",",$resultstr);

   $sql ="Update ".$table." SET ".$values."WHERE id=:id";

   
   
   $query =  $conn->prepare($sql);

   $query->bindParam(':id',$id);

   foreach($data as $key=>$val){
        $query->bindValue(':'.$key,$val);
        
    }
    $updateData = $query->execute();
     
   
   
   return $updateData;

 }


What I have tried:

try to make an update function .... but getting errors
Posted
Updated 8-Jun-18 2:37am
Comments
Richard MacCutchan 8-Jun-18 8:45am    
Look at line 25 and check the values of the variables that your code is trying to reference.

1 solution

To verify what went wrong just print the $sql string and check if it is a valid SQL command.

The error is probably sourced by the missing space before the WHERE keyword. So change that to
PHP
$sql ="Update ".$table." SET ".$values." WHERE id=:id";
// Insert space here                    ^

Other possible error sources are that one or more of your $keys contains a comma or space.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900