2012-04-15 11 views
3

ちょっと私は配列を通してPDOを使ってデータベース挿入を組み立てようとしていますが、どこかでそれを見逃しています。配列は 連想配列です。エラースローは次のとおりです。私が使用しているPHP PDO配列をバインドする

Fatal error: Uncaught exception \'PDOException\' with message \'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens\' in /var/www/html/themonastery.org/mot/receiver.php:70 
Stack trace: 
#0 /var/www/html/themonastery.org/mot/receiver.php(70): PDOStatement->execute() 
#1 {main} 
    thrown in /var/www/html/themonastery.org/mot/receiver.php on line 70 

コードは次のとおりです。

/** PDO Stuff **/ 

    //require and instantiate pdo instance 
    require_once "dependancies/pdo.func.php"; 
    $dbh = pdo_connect(); 

    //implode query 
    $keys = implode(',', array_keys($clean)); 
    $vals = implode(',', array_fill(0, count($clean), '?')); 

    $insert = array_values($clean); 

    //pdo prepare 
    $sth = $dbh->prepare("INSERT INTO backupDB ($keys) VALUES ($vals)"); 

    //set loop condition 
    $waiting = true; 
    while($waiting) { 
     try { 
     $dbh->beginTransaction(); 

     $i=1; 
     foreach($clean as $insert) { 

      // bindvalue is 1-indexed, so $k+1 
      $sth->bindValue($i++, $insert, PDO::PARAM_STR); 

      $sth->execute(); 
      sleep(1); 
     } 

     $dbh->commit(); 
     $waiting = false; 

     } catch(PDOException $e) { 
     if(stripos($e->getMessage(), 'DATABASE IS LOCKED') !== false) { 
      //sleep for 0.25 seconds and try again. 
      $dbh->commit(); 
      usleep(250000); 
     } else { 
      $dbh->rollBack(); 
      throw $e; 
     } 
     } 
    } 

はここで連想配列、

array (
    'full_name' => 'First Middle Last Suffix', 
    'first_name' => 'First', 
    'middle_name' => 'Middle', 
    'last_name' => 'Last Suffix', 
    'address' => 'The Address', 
    'city' => 'City', 
    'state' => 'State Abbr', 
    'zip' => 'Zip code', 
    'country' => 'Country Abbr', 
    'email' => '[email protected]', 
    'password' => 'd41d8cd98f00b204e9800998ecf8427e', 
    'ordinationDate' => '2012-04-15', 
    'birthday' => '1982-14-01', 
    'isValidAge' => '1', 
) 

、リクエストによってはここ$keys$vals

ののvar_dumpだです
$keys = string(123) "full_name,first_name,middle_name,last_name,address,city,state,zip,country,email,password,ordinationDate,birthday,isValidAge" 
$vals = string(27) "?,?,?,?,?,?,?,?,?,?,?,?,?,?" 

ここでは、このDB

id full_name first_name middle_name last_name address city state zip country email password ordinationDate birthday isValidAge sex timestamp ulc_edit_time osc_sync guid 
+1

'var_dump'' $ keys'と '$ vals'でお願いします。 –

+0

@GabrielSantosが更新されました – ehime

+0

'$ keys'と' $ vals'はokです。すべてのフィールド名が正しいことは確かですか? –

答えて

2

変更から列名です:これまで

$i=1; 
foreach($clean as $insert) { 

    // bindvalue is 1-indexed, so $k+1 
    $sth->bindValue($i++, $insert, PDO::PARAM_STR); 

    $sth->execute(); 
    sleep(1); 
} 

$i=1; 
foreach($clean as $insert) { 

    // bindvalue is 1-indexed, so $k+1 
    $sth->bindValue($i++, $insert, PDO::PARAM_STR); 

    sleep(1); 
} 

$sth->execute(); 

PDO ::実行()すべてのbindValuesの終わりにする必要がありますが、 ()(http://php.net/manual/en/pdostatement.execute.php#example-995を参照)

また、私は正しいデータ型あなたのケースでいくつかの変更があります):

public function bindValue($key = null, $value = null) { 
    if($key == null) { 
     return; 
    } 

    if(is_int($value)) { 
     $param = PDO::PARAM_INT; 
    } elseif(is_bool($value)) { 
     $param = PDO::PARAM_BOOL; 
    } elseif(is_null($value)) { 
     $param = PDO::PARAM_NULL; 
    } elseif(is_string($value)) { 
     $param = PDO::PARAM_STR; 
    } else { 
     $param = FALSE; 
    } 

    $this->_query->bindValue($key, $value, $param); 
} 
+0

美しく働いた、ありがとうガブリエル! – ehime

+0

あなたは歓迎です=) –

関連する問題