Hi

This is kind of a follow-on from this thread :
http://phpbuilder.com/board/showthread.php?t=10353998&highlight=PDO+prepare

I've finally got round to trying out the prepare thing on multiple inserts from inside a loop, like this :

  if(!empty($resNums)){

$sth = $conn->prepare("insert into menu_items (item_id, menu_id, menu_item_lang, menu_item_text, menu_item_online, menu_item_order) values (:item_id, :menu_id, :menu_item_lang, :menu_item_text, :menu_item_online, :menu_item_order)");

$sth->bindParam(':item_id', $item_id, PDO::PARAM_NULL);
$sth->bindParam(':menu_id', $menu_id, PDO::PARAM_INT);
$sth->bindParam(':menu_item_lang', $menu_item_lang, PDO_PARAM_STR, 4);
$sth->bindParam(':menu_item_text', $menu_item_text, PDO_PARAM_STR, 128);
$sth->bindParam(':menu_item_online', $menu_item_online, PDO::PARAM_INT); (line 833)
$sth->bindParam(':menu_item_order', $menu_item_order, PDO::PARAM_INT);

foreach($resNums as $v){
  foreach($langsAv as $w){
    $f = 0;
    for($c=1;$c<=count($res);$c++){
      if (($res[$c]['menu_id']==$v['menu_id']) && ($res[$c]['menu_item_lang']==$w['lang_id'])){
        $f = 1;
        break;
      }
    }
    if($f == 0){
      $titreTmp = "**".strtolower($v['menu_item_text'])."**";
      $item_id = null;
      $menu_id = $v['menu_id'];
      $menu_item_lang = $w['lang_id'];
      $menu_item_text = $titreTmp;
      $menu_item_online = 1;
      $menu_item_order = 1;
      $sth->execute();
      $err = $sth->errorInfo();
      print_r($err); (line 855)
    }
  }  
}


$stmt = null;

  } 

for info, this is how it would have appeared using the old system :

INSERT INTO `menu_items` (`item_id`, `menu_id`, `menu_item_lang`, `menu_item_text`, `menu_item_online`, `menu_item_order`) VALUES 
(NULL, 99, 'de', '**accueil**', 1, 1),
(NULL, 100, 'de', '**qui sommes-nous ?**', 1, 1),
(NULL, 102, 'de', '**emploi**', 1, 1),
(NULL, 103, 'de', '**nous trouver**', 1, 1),
(NULL, 113, 'de', '**le mot du président**', 1, 1),
(NULL, 114, 'de', '**notre histoire**', 1, 1),
(NULL, 115, 'de', '**notre organisation**', 1, 1),
(NULL, 115, 'en', '**notre organisation**', 1, 1),
(NULL, 119, 'de', '**les métiers**', 1, 1),
(NULL, 120, 'de', '**candidatures spontanées**', 1, 1),
(NULL, 122, 'de', '**nos offres d emploi**', 1, 1),
(NULL, 123, 'de', '**recherche par commune d'une agence**', 1, 1),
(NULL, 123, 'en', '**recherche par commune d'une agence**', 1, 1),
(NULL, 124, 'de', '**liste des agences**', 1, 1),
(NULL, 124, 'en', '**liste des agences**', 1, 1),
(NULL, 125, 'de', '**ressources humaines**', 1, 1),
(NULL, 126, 'de', '**notre politique rh**', 1, 1),
(NULL, 127, 'de', '**accueil des nouveaux salariés**', 1, 1),
(NULL, 128, 'de', '**la formation**', 1, 1),
(NULL, 129, 'de', '**la prévention**', 1, 1);

BUT I'm getting a load of error messages and I've looked on the interwebz and I still don't understand what I'm doing wrong

the 2 types of error I'm getting are

Warning: PDOStatement::bindParam() expects parameter 3 to be long, string given in /homez.331/passiontd/includes/inc_functions.php on line 833

and

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /homez.331/passiontd/includes/inc_functions.php on line 855 
Array ( [0] => HY093 )

I really don't understand what the error messages mean - has anyone got any ideas what I'm doing wrong here ??

thanks

    ha! I just noticed the lines

    $sth->bindParam(':menu_item_lang', $menu_item_lang, PDO_PARAM_STR, 4);
    $sth->bindParam(':menu_item_text', $menu_item_text, PDO_PARAM_STR, 128);

    were differnt to the others so I replaced

     PDO_PARAM_STR

    with

     PDO::PARAM_STR

    and it worked !

    that's what you get for blindly pasting in chunks of code found on the web without thinking about what it's doing ....

      18 days later
      Write a Reply...