Jump to content
MakeWebGames

bindParams bindAllParams


Sim

Recommended Posts

I know I posted this before

 

Instead of binding all params one at a time to have a function like this:

It works,but confused

//1st lime dont matter
$insert->prepare($query);

$this->bindAllParams($insert, $this->methodData);

$insert->execute();

public function bindAllParams(&$db, $formData) {
  
  $count = 0;
  $bindedText = array(
   "name", "desc", "points", "hours",
  "multiplier");
  
  $bindedCheckBoxes = array(
   "offTheRadar","cash", 
   "exp", "crimes", 
   "chase");
  
  foreach($formData as $key => $value)
  {
    //text fields  
    if(in_array($key, $bindedText))
    {
//THIS HERE IS WHAT IM TALKING ABOUT
       debug("key " . $key . " found value" . $value);
      $db->bindParam(":". $key, $this->methodData->{$key});
    //    $db->bindParam(":". $key, $value);
     
    }
    
  }
 
 $check = 'y';
 $noCheck = 'n';
 
  foreach($bindedCheckBoxes as $key)
  {
    
    //check
    if(!empty($formData->{$key}))
    { 
        debug("key " . $key . " checked" . $value);
        $db->bindParam(":". $key, $check);
    }  
    else {
       debug("key " . $key . " not checked" . $value); 
       $db->bindParam(":" . $key, $noCheck);
    }
    
  }
 
}

 

 

So this will I input the correct values

      $db->bindParam(":". $key, $this->methodData->{$key});

This will Insert a 0 or 1 but debug value displays the real value

$db->bindParam(":". $key, $value);

It has me puzzled if anyone has the answer. 

 

 

But After I am done a few things I will end up making this function determine if form data or not to automate the process instead of needing arrays to check what too bind. To help speed up the process of future admin mods dealing with form data and the database.

Edited by Sim
Link to comment
Share on other sites

#allLimesMatter
With PDO, you can pass an array of key-pair parameters to an execute() call.

$stmt = $pdo->query('INSERT INTO users (username, level) VALUES (:name, :level)');
$stmt->execute([
    ':name' => $someName,
    ':level' => $nextLevelMaybe
]);


Alternatively, you can also use question-mark placeholders and pass parameters in the same order

$stmt = $pdo->query('UPDATE some_table SET some_value = ?, some_other_value = ? WHERE some_criteria = ?');
$stmt->execute([$firstVal, $secondVal, $criteriaVal]);


Perhaps this information may be useful to you during your wrapper writes

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...