Can't debug random error
I have a web-based custom ticket sales I made for a friend and whenever I tried it (on any browser) it worked perfectly fine. However when it went live yesterday some people tried to book tickets and they would receive their email stating they had their seats booked (everything was ok) but the system would only record some of those seats.
Let's say that if they booked A1 - A10 they got an email saying they booked those but the system would only book A1 - A5 (or A6 or so, it seems random).
Also, this wouldn't happen with EVERY person it just happens randomly (at least I haven't found a proper reason for it to happen yet).
The code is here when I book the tickets:
$cantTotal=0;
$boletosF1 = $_POST['funcion1Hidden'];
if($boletosF1!=""){
$lugares = explode(" ", $boletosF1);
$cant = count($lugares);
$cantTotal += $cant;
$f1 = "";
$sqlAP = "SELECT * FROM apartados";
if ($resultAP = mysql_query($sqlAP)) {
while ($rowAP = mysql_fetch_array($resultApP)) {
$f = $rowAP['funcion'];
$lugar = $rowAP['lugar'];
$count++;
if ($f == "F1") {
$f1.= ( $lugar . " ");
}
}
}
$sqlPag2 = "SELECT * FROM pagados";
if ($resultPag2 = mysql_query($sqlPag2)) {
while ($rowPag2 = mysql_fetch_array($resultPag2)) {
$f = $rowPag2['funcion'];
$lugar = $rowPag2['lugar'];
$count++;
if ($f == "F1") {
$f1.= ( $lugar . " ");
}
}
}
$func = explode(" ", $f1);
$cantUtilizados = count($func);
$repetidos = 0;
for ($int = 0; $int < $cant; $int++) {
for ($r = 0; $r < $cantUtilizados; $r++) {
if ($func[$r] == $lugares[$int]) {
$repetidos++;
}
}
}
if ($repetidos > 0) {
redirect("apartadoBoletos.php?error=1");
}
}
$boletosF2 = $_POST['funcion2Hidden'];
if($boletosF2!=""){
$lugares2 = explode(" ", $boletosF2);
$cant2 = count($lugares2);
$cantTotal+=$cant2;
$f2 = "";
$sqlAP = "SELECT * FROM apartados";
if ($resultAP = mysql_query($sqlAP)) {
while ($rowAP = mysql_fetch_array($resultApP)) {
$f = $rowAP['funcion'];
$lugar = $rowAP['lugar'];
$count++;
if ($f == "F2") {
$f2.= ( $lugar . " ");
}
}
}
$sqlPag2 = "SELECT * FROM pagados";
if ($resultPag2 = mysql_query($sqlPag2)) {
while ($rowPag2 = mysql_fetch_array($resultPag2)) {
$f = $rowPag2['funcion'];
$lugar = $rowPag2['lugar'];
$count++;
if ($f == "F2") {
$f2.= ( $lugar . " ");
}
}
}
$func2 = explode(" ", $f2);
$cantUtilizados = count($func2);
for ($int = 0; $int < $cant2; $int++) {
for ($r = 0; $r < $cantUtilizados; $r++) {
if ($func2[$r] == $lugares2[$int]) {
$repetidos++;
}
}
}
if ($repetidos > 0) {
redirect("apartadoBoletos.php?error=1");
}
}
function redirect($url) {
echo "<script language=\"JavaScript\"> window.location='$url'; </script>";
}
$totalAPagar=120*$cantTotal;
and when I save them to the Database and send the email:
$boletosF1 = $_POST['boletosF1'];
$boletosF2 = $_POST['boletosF2'];
$nombre = $_POST['name'];
$correo = $_POST['mail'];
$cant1=0;
$cant2=0;
if($boletosF1!=""){
$lugares1 = explode(" ", $boletosF1);
echo $lugares1;
$cant1 = count($lugares1);
echo $cant1;
$f1 = "";
$sqlAP = "SELECT * FROM apartados";
if ($resultAP = mysql_query($sqlAP)) {
while ($rowAP = mysql_fetch_array($resultApP)) {
$f = $rowAP['funcion'];
$lugar = $rowAP['lugar'];
$count++;
if ($f == "F1") {
$f1.= ( $lugar . " ");
}
}
}
$sqlPag2 = "SELECT * FROM pagados";
if ($resultPag2 = mysql_query($sqlPag2)) {
while ($rowPag2 = mysql_fetch_array($resultPag2)) {
$f = $rowPag2['funcion'];
$lugar = $rowPag2['lugar'];
$count++;
if ($f == "F1") {
$f1.= ( $lugar . " ");
}
}
}
$func1 = explode(" ", $f1);
$cantUtilizados1 = count($func1);
$repetidos = 0;
for ($int = 0; $int < $cant1; $int++) {
for ($r = 0; $r < $cantUtilizados1; $r++) {
if ($func1[$r] == $lugares1[$int]) {
$repetidos++;
}
}
}
if ($repetidos > 0) {
redirect("apartadoBoletos.php?error=1");
}
}
if($boletosF2!=""){
$lugares2 = explode(" ", $boletosF2);
echo $lugares2;
$cant2 = count($lugares2);
echo $cant2;
$f2 = "";
$sqlAP = "SELECT * FROM apartados";
if ($resultAP = mysql_query($sqlAP)) {
while ($rowAP = mysql_fetch_array($resultApP)) {
$f = $rowAP['funcion'];
$lugar = $rowAP['lugar'];
$count++;
if ($f == "F2") {
$f2.= ( $lugar . " ");
}
}
}
$sqlPag2 = "SELECT * FROM pagados";
if ($resultPag2 = mysql_query($sqlPag2)) {
while ($rowPag2 = mysql_fetch_array($resultPag2)) {
$f = $rowPag2['funcion'];
$lugar开发者_开发知识库 = $rowPag2['lugar'];
$count++;
if ($f == "F2") {
$f2.= ( $lugar . " ");
}
}
}
$func2 = explode(" ", $f2);
$cantUtilizados2 = count($func2);
$repetidos = 0;
for ($int = 0; $int < $cant2; $int++) {
for ($r = 0; $r < $cantUtilizados2; $r++) {
if ($func2[$r] == $lugares2[$int]) {
$repetidos++;
}
}
}
if ($repetidos > 0) {
redirect("apartadoBoletos.php?error=1");
}
}
$rand = rand(0, 500);
$clave = sha1($rand . date("shd"));
$clave = substr($clave, 5, 5);
//insertar nuevo cliente
//mysql_query('BEGIN');
$sql = "INSERT INTO cliente(`idCliente`,`fecha`,`Nombre`,`correo`)VALUES('$clave',NOW(),'$nombre','$correo')";
$result = mysql_query($sql);
//crear arrays para guardar lugares
//insertar apartado F1
for ($i = 0; $i < $cant1; $i++) {
$sqlF1 = "INSERT INTO apartados(`lugar`,`idCliente`,`funcion`)VALUES('$lugares1[$i]','$clave','F1')";
$resultF1 = mysql_query($sqlF1);
}
for ($i = 0; $i < $cant2; $i++) {
$sqlF1 = "INSERT INTO apartados(`lugar`,`idCliente`,`funcion`)VALUES('$lugares2[$i]','$clave','F2')";
$resultF1 = mysql_query($sqlF1);
}
if (mysql_error ()) {
//echo mysql_error ();
// mysql_query('ROLLBACK');
redirect("apartadoBoletos.php?error=2");
} else {
$totalAPagar=($cant1+$cant2)*120;
// mysql_query('COMMIT');
$to = $correo;
$dia=date("d-m-Y");
//echo $dia;
//User info
$subject = "Boletos"; //The default subject. Will appear by default in all messages.
//The message to be received in the inbox
$msg .= "Estimad@ $nombre: \n\n";
$msg .= "Clave: $clave " . "\r\n\n"; //the message itself
$msg .= "Boletos 28 de Junio: $boletosF1 " . "\r\n";
$msg .= "Boletos 27 de Junio: $boletosF2 " . "\r\n\n";
$msg .= "Total a pagar: $ $totalAPagar.00 " . "\r\n\n";
//$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= "From: no-reply@booking.com" . "\r\n";
$header .= "Reply-To: no-reply@booking.com" . "\r\n";
$header .= "Return-Path: $email" . "\r\n";
mail($to, $subject, $msg, $header);
}
function redirect($url) {
echo "<script language=\"JavaScript\"> window.location='$url'; </script>";
}
So far I can't find the error and I've got to since this should have started working yesterday but everything had to be stopped =/ I really need help here please.
Also, something that might help, I think the error could be related to the amount of users at a given time however since it just happens to some of the persons that use this I really can't say that's the reason.
If you aren't sure where the error is occurring, you could add extensive logging for a while to see if you can figure it out.
For example, when the user clicks save, or buy tickets, or whatever, you could write to the log file the data that the user has input on the form.
Then, you could log again when you send the purchase information to the database. Record the SQL or stored procedure name and parameter values.
This kind of extensive logging tends to hurt performance, but when you are in a desperate situation, sometimes it's worthwhile.
As soon as you have an idea of the problem, or think you have logged enough data to analyze, you can turn the logging back off.
This is really a comment, but it's too code centric to fit in one.
Consider rewriting your code so it actually uses SQL.
This code
$f1 = "";
$sqlAP = "SELECT * FROM apartados";
if ($resultAP = mysql_query($sqlAP)) {
while ($rowAP = mysql_fetch_array($resultApP)) {
$f = $rowAP['funcion'];
$lugar = $rowAP['lugar'];
$count++;
if ($f == "F1") {
$f1.= ( $lugar . " ");
}
}
}
Looks silly, you're not using SQL at all.
Rewrite it into this:
$sqlAP = "SELECT group_concat(lugar SEPARATOR ' ') as lugar
, count(*) as count1
FROM apartados WHERE funcion = 'F1'";
if ($resultAP = mysql_query($sqlAP)) {
$rowAP = mysql_fetch_array($result_AP);
$count = $rowAP['count1'];
$lugar = $rowAP['lugar'];
}
Much faster and much cleaner.
精彩评论