2012-02-11 4 views
0
<?php 

require_once "includes/db_data_inc.php"; 

try 
{ 
    /* In this way I get a db connection handle */ 
    $DBH = new PDO("mysql:host=$db_host;port=8889;dbname=$db_name",$db_user,$db_pass); 

} 
catch (PDOException $pdoe) 
{ 
    error_log($pdoe->getMessage()); 
    die("Failed to connect to the database."); 
} 

$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$DBH->setAttribute(PDO::ATTR_AUTOCOMMIT,false); 

try 
{ 
    $success_msg = NULL; 
    $unsucess_msg = NULL; 

    $query = "INSERT INTO users (name,surname,address,birth_place,province,birthdate,sex,treatment) VALUES (?)"; 
    $SHT = $DBH->prepare($query); 

    $SHT->bindParam(1, $_POST['name']); 
    $SHT->bindParam(2, $_POST['surname']); 
    $SHT->bindParam(3, $_POST['address']); 
    $SHT->bindParam(4, $_POST['birth_place']); 
    $SHT->bindParam(5, $_POST['province']); 
    $SHT->bindParam(6, $_POST['dt']); 
    $SHT->bindParam(7, $_POST['gender']); 
    $SHT->bindParam(8, $_POST['select']); 

    $DBH->beginTransaction(); 

    if($SHT->execute()) 
    { 
     $DBH->commit(); 
     $success_msg = "The emergency call was correctly sent..."; 
    } 
    else 
    { 
     $unsucess_msg = "It couldn't estabilished a connection to call center..."; 
    } 
} 
catch(PDOException $pdoe) 
{ 
    $DBH->rollBack(); 
} 


/* Close the db connection */ 
$DBH = null; 

?>mysqlの接続機能のpdo

ここでは、MySQL DBとINSERTクエリに接続するために、私のmysqlの接続機能です。コードは動作しません。エラーがありますか?私はPHPコンソールから何のエラーも受けませんが、ページには何の結果も見られません。

私は、コンソール上で働いていたし、このエラーを得た:

Invalid parameter number: number of bound variables does not match number of tokens 

それが何を意味するのでしょうか?

+0

利用何か;'例外を取得します。何らかの形でトランザクションをロールバックして実際のエラーを完全に無視するのは無駄です... – CodeZombie

答えて

0

'?'後で渡す8つのパラメータと一致しません。

PDOStatement::bindParamメソッドの例を見てください。

使用何かのように: `のvar_dump($ PDOE)のような

$query = "INSERT INTO users (name,surname,address,birth_place,province,birthdate,sex,treatment) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; 
+0

だから私は8を入れました '?列をなして...? –

+0

またはbindParam( ':name'、$ _POST ['name'])のようなプレースホルダを作成し、substitue:nameを '?'にすることができます。右? –

+0

@eng_mazzy:私のリンクの例を見るだけで、すべての質問に答える必要があります。 – CodeZombie