I have two webpages, the one that you fill the information and another that processes it.
My firstpage is plain and simple HTML with jQuery and AJAX, as soon as the user clicks a button, it executes the following piece of code:
$("#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;
}
});
});
I have no trouble with that specific part of the web page, it sends the variable, and if notice I’m confirming that by an alert dialog, and all the information is valid and correct.
The problem comes with the PHP web page,
I have a span tag in my HTML that reports errors if any.
When I click submit, it only shows:
Error: Array()
I put that error string if the transaction is not executed, but I’m not sure why it isn’t executed, and the errorInfo() method only reports: Array()
My PHP Code is as follows:
<?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";
?>
At firs I thought that I wasn’t receiving POST variables, but if I add a:
print_r($_POST);
It prints the POST array with all the right values
It doesn’t seem to be complaining about anything, but the transaction is not commited to the database.
On the other hand, I have other web pages that follow the same logic, the same code, and those pages work as expected.
It is also the first time I’m using PDO, I usually used MySQLi
PDO’s
errorInforeturns an array, not a string. Usevar_exportorprint_rto turn it into a string.Additionally, in my opinion it’s a good idea to set PDO’s error mode to throw exceptions when a query cannot be executed, instead of checking each query’s result. Then you can wrap an entire transaction in a try/catch and rollback the transaction if something goes wrong.