Need help with a function that I am making

0 Savad Dekrup · March 17, 2015
Here is the code

function update($table,$fields = array()){

$size = count($fields);
$sqlString = '';
$queryValue = '';

foreach($fields as $field => $value){
$size--; // size = 1
$sqlString .= $field . ' = ' . '?'; // '\'' . $value . '\''

$queryValue .= '\'' . $value . '\'';
 
   if ($size) // size = 1 means true
   {
       $sqlString .= ' , '; // username = newUsername , 
       $queryValue .= ' ';
   }

}

$explodeQueryValue = explode(" ", $queryValue);

$sqlQuery = "UPDATE " . $table . " SET " . $sqlString;

global $pdo;
$query = $pdo->prepare($sqlQuery);

$bindingSize = count($fields);

for($i = 1; $i < $bindingSize; $i++){
$query->bindValue($i, $explodeQueryValue[$i-1]);
}

$query->execute();

}
update('users', array('username' => 'username', 'password' => 'password'));

What I get is this



Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens'

*SIGH*
I have spent 1 - 2 hours try find the issue, but it is no use. I am done. 
LOL


If you could help me, I would really appreciate your time. 

#HappyCoding

Post a Reply

Replies

Oldest  Newest  Rating
0 Savad Dekrup · March 17, 2015

for($i = 1; $i < $bindingSize; $i++){
$query->bindValue($i, $explodeQueryValue[$i-1]);
}

What the heck with 
https://www.thenewboston.com/images/emoticons/wink.png 
0 Savad Dekrup · March 17, 2015

function update($table,$fields = array()){

$size = count($fields);
$sqlString = '';
$queryValue = '';

foreach($fields as $field => $value){
$size--; // size = 1
$sqlString .= $field . ' = ' . '?';

$queryValue .= '\'' . $value . '\'';

if ($size) // size = 1 means true
{
$sqlString .= ' , '; // username = newUsername ,
$queryValue .= ' ';
}

}

$explodeQueryValue = explode(" ", $queryValue);

$sqlQuery = "UPDATE " . $table . " SET " . $sqlString;

global $pdo;
$query = $pdo->prepare($sqlQuery);

$bindingSize = count($fields);

for($i = 1; $i < $bindingSize; $i++){
$query->bindValue($i, $explodeQueryValue [$i-1] ) ;
}

$query->execute();

}
update('users', array('username' => 'username', 'password' => 'password'));
+1 Mohamad Mirohamadi · March 18, 2015
I don't know too much about pdo but this is a tweak that I made for your function:

function update($table,$fields = array()){
    $keys = array_keys($fields);
    $values = array_map('mysql_real_escape_string', array_values($fields));
    $sqlQuery = 'UPDATE `'.$table.'` set `'.implode('` = ? ,`', $keys).'` = ?';

try{
    global $pdo;
    $query = $pdo->prepare($sqlQuery);

    foreach ($values as $key => $value)
            $query->bindValue($key , $value);
     $query->execute();       
 }catch(Exception $e)
 {
     echo $e->getMessage();
 }
}
0 Savad Dekrup · March 18, 2015
Thank you for the response, I actually fixed the problem. After Spending one more hour >_>


It was this:


for($i = 1; $i < $bindingSize; $i++){
$query->bindValue($i, $explodeQueryValue [$i-1] ) ;
}



apparently, I was stupid enough. I forgot that I started with 1 not 0
for($i = 1; $i bindValue($i, $explodeQueryValue [$i-1] ) ;
}
+1 Mohamad Mirohamadi · March 18, 2015
so if bindValue  start with 1 you should change my foreach to this:


foreach ($values as $key => $value)
           $query->bindValue($key+1 , $value);
  • 1

PHP

107,335 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators