2012-05-13 10 views
1

私は2つのWebページを持っています.1つは情報を埋め、もう1つはそれを処理します。私FIRSTPAGEは、すぐにユーザーがボタンをクリックすると、jQueryとAJAXと無地でシンプルなHTMLで 、それは次のコードを実行:PHP PDOモジュールはトランザクションをコミットせず、理由の報告に失敗します

$("#registrar-tienda").click(function() { 
    var categoria = $("#categoria").val(); 
    var nombre = $("#nombre").val(); 
    var direccion = $("#direccion").val(); 
    var codigo_postal = $("#codigo").val(); 
    var poblacion = $("#poblacion").val(); 
    var provincia = $("#provincia").val(); 
    var latitud = $("#latitud").val(); 
    var longitud = $("#longitud").val(); 
    var telefono = $("#telefono").val(); 
    var categoria = $("#categoria").val(); 
    var email = $("#email").val(); 
    var web = $("#web").val(); 
    var eco_punto = $("#eco-punto").val(); 
    var compra_minima = $("#compra-minima").val(); 
    var maximo_acumulable = $("#maximo-acumulable").val(); 
    var cliente_hash = $("#cliente-hash").val(); 
    alert(nombre+direccion+codigo_postal+poblacion+provincia+latitud+longitud+telefono+email+web+eco_punto+compra_minima+maximo_acumulable+cliente_hash); 
    $.ajax({ 
     type:"POST", 
     url:"modulos/tiendas/operaciones.php", 
     data:{categoria:categoria,nombre:nombre,direccion:direccion,codigo_postal:codigo_postal,poblacion:poblacion,provincia:provincia,latitud:latitud,longitud:longitud,telefono:telefono,categoria:categoria,email:email,web:web,eco_punto:eco_punto,compra_minima:compra_minima,maximo_acumulable:maximo_acumulable,cliente_hash:cliente_hash} 
     }).done(function(msg) { 
     if(msg == "Tienda agregada") { 
      document.location.reload(true); 
     } else { 
      document.getElementById("errores").innerHTML = msg; 
      } 
     }); 
}); 

私は、Webページの特定の部分で支障がありません、それは変数を送信します。通知があれば、警告ダイアログでそれを確認し、すべての情報が有効で正しいものです。

PHPウェブページに問題があります。 エラーがある場合はエラーを報告するHTMLにスパンタグがあります。 私が提出]をクリックすると、それだけを示しています。

エラー:配列()

トランザクションが実行されていない場合、私はそのエラー文字列を入れて、それが実行されていない理由を私はわからない、とerrorInfo()メソッドは、レポートのみ:次のようにアレイ()

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

もみで
<?php 
require('../../includes/etc/connection.php'); 
isset($_POST['categoria']) or die ("No hay categoria definida"); 
isset($_POST['nombre']) or die("No hay nombre de negocio"); 
isset($_POST['direccion']) or die("No hay direccion de negocio"); 
isset($_POST['poblacion']) or die ("No hay poblacion de negocio"); 
isset($_POST['codigo_postal']) or die("No hay codigo postal de negocio"); 
isset($_POST['provincia']) or die("No hay provincia"); 
isset($_POST['latitud']) or die("No hay latitud"); 
isset($_POST['longitud']) or die("No hay longitud"); 
isset($_POST['telefono']) or die("Sin telefono"); 
isset($_POST['categoria']) or die("No hay categoria"); 
isset($_POST['eco_punto']) or die("No hay valor del ecopunto"); 
isset($_POST['maximo_acumulable']) or die("No hay maximo acumulable"); 
isset($_POST['compra_minima']) or die("No hay compra minima"); 
isset($_POST['cliente_hash']) or die("Se tiene que seleccionar un cliente"); 

if($_POST['nombre'] == "Nombre del establecimiento"): die(0); endif; 
if($_POST['direccion'] == "Direccion"): die(0); endif; 
if($_POST['poblacion'] == "Poblacion"): die(0); endif; 
if($_POST['codigo_postal'] == "Codigo Postal"): die(0); endif; 
if($_POST['provincia'] == "Provinicia"): die(0); endif; 
if($_POST['telefono'] == "Telefono"): die(0); endif; 
if($_POST['email'] == "E-mail"): die(0); endif; 

$hash = md5($_POST['nombre'].$_POST['latitud'].$_POST['longitud'].date("Y-m-d H:M:s")); 
$fecha_registro = date("Y-m-d"); 
$strSQL = "INSERT INTO `negocios`(`hash`,`cliente_hash`,`fecha_registro`,`nombre`,`apellido`,`direccion`,`codigo_postal`, `poblacion`, `provincia`, `latitud`,`longitud`,`telefono`, `email`, `web`,`categoria`,`valor_ecopunto`,`maximo_acumulable`,`compra_minima`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
if(!($sth = $dbh->prepare($strSQL))) { 
    die("Statement not prepared:".$dbh->errorInfo()); 
} 

if(!$dbh->beginTransaction()){ 
    die ("Transaction didn't begin: ".$dbh->errorInfo()); 
} 

$sth->bindParam(1,$hash,PDO::PARAM_STR,32) or die("Not binded"); 
$sth->bindParam(2,$_POST['cliente_hash'],PDO::PARAM_STR,32) or die("Not binded"); 
$sth->bindParam(3,$_POST['fecha_registro'],PDO::PARAM_STR,15) or die("Not binded");; 
$sth->bindParam(4,$_POST['nombre'],PDO::PARAM_STR,150) or die("Not binded");; 
$sth->bindParam(5,$_POST['direccion'],PDO::PARAM_STR,150) or die("Not binded");; 
$sth->bindParam(6,$_POST['codigo_postal'],PDO::PARAM_INT,9) or die("Not binded");; 
$sth->bindParam(7,$_POST['poblacion'],PDO::PARAM_STR,50) or die("Not binded");; 
$sth->bindParam(8,$_POST['provincia'],PDO::PARAM_STR,50) or die("Not binded");; 
$sth->bindParam(9,$_POST['latitud'],PDO::PARAM_STR,10) or die("Not binded");; 
$sth->bindParam(10,$_POST['longitud'],PDO::PARAM_STR,10) or die("Not binded");; 
$sth->bindParam(11,$_POST['telefono'],PDO::PARAM_STR,20) or die("Not binded");; 
$sth->bindParam(12,$_POST['email'],PDO::PARAM_STR,100) or die("Not binded");; 
$sth->bindParam(13,$_POST['web'],PDO::PARAM_STR,100) or die("Not binded");; 
$sth->bindParam(14,$_POST['categoria'],PDO::PARAM_INT,2) or die("Not binded");; 
$sth->bindParam(15,$_POST['eco_punto'],PDO::PARAM_INT,4) or die("Not binded");; 
$sth->bindParam(16,$_POST['maximo_acumulable'],PDO::PARAM_INT,4) or die("Not binded");; 
$sth->bindParam(17,$_POST['compra_minima'],PDO::PARAM_INT,5) or die("Not binded");; 
if(!$sth->execute()){ 
    die("Error: ".$sth->errorInfo()); // Error reported in the HTML webpage 
} 
$dbh->commit(); 
echo "Tienda Agregada"; 
?> 

私が追加した場合、私はPOST変数を受け取っていなかったと思ったが、:

print_r($_POST);

それは何でも文句していないようですが、トランザクションがデータベースにコミットされていないすべての権利値

とPOST配列を出力します。

一方、私は、同じロジック、同じコード、およびそれらのページが期待どおりに動作する他のWebページを持っています。

それはまた、私はPDOを使用しています初めてです、私は通常MySQLiを

+1

PDOの 'errorInfo'は文字列ではなく配列を返します。 ['var_export'](http://us.php.net/var_export)または[' print_r'](http://us.php.net/print_r)を使用して文字列に変換します。 – DCoder

+0

Gosh ... PHP.netのPDO errorInfo Webページを読んでいて、配列ではなく文字列ではないことに気づいたことはありません...私はとても馬鹿だと感じています。それを指摘していただきありがとうございます! 回答として投稿し、それを最高のものとして選択できますか? – ILikeTacos

答えて

3

PDOのerrorInfo戻り、配列ではなく、文字列を使用していました。 var_exportまたはprint_rを使用して文字列に変換します。


また、私の意見では、代わりに各クエリの結果を確認する、クエリを実行することができませんset PDO's error mode to throw exceptionsすることをお勧めします。次に、トランザクション全体をtry/catchでラップし、問題が発生した場合はトランザクションをロールバックすることができます。

関連する問題