I create a PHP that execute multiples query. I wanna make a transaction, because i need to save all the query if everything works ok, if not make a rollback. Is critical save all the data. I don't know how to make the transaction. I've been following that:PHP + MySQL transactions examples but it's not working.
This is my php:
<?php
require_once __DIR__ . '/connect_to_database.php';
$db = new DB_CONNECT();
$response = array();
$horaSeleccionada= $_POST['horaSeleccionada'];
$fechaElegida= $_POST['fechaElegida'];
$data1FK= $_POST['data1FK'];
$reservado= $_POST['reservado'];
$usuarioFK= $_POST['usuarioFK'];
$nombreAuxiliar= $_POST['nombreAuxiliar'];
$fechaDeReserva= $_POST['fechaDeReserva'];
$telefonoAux= $_POST['telefonoAux'];
$anioHoy = (int) $_POST['anioHoy'];
$filasAfectadas = 0;
$consultasRealizadas = 0;
$fecha = date_create($fechaElegida);
$anioAuxiliar = (int) $fecha->format("Y");
while($anioHoy == $anioAuxiliar){
$fechaAGuardar = (String) $fecha->format('Y-m-d');
$query_search = "insert into table(hora, data1FK, usuarioFK, reservado, fecha, nombreAuxiliar, fechaDeReserva, telefonoAux) values ('$horaSeleccionada','$data1FK','$usuarioFK','$reservado','$fechaAGuardar','$nombreAuxiliar','$fechaDeReserva','$telefonoAux')";
$result = mysql_query($query_search) or die(mysql_error());
if(mysql_affected_rows()>0){
$filasAfectadas++;
}
else{
$response["success"] = 0;
$response["mensaje"] = "Hubo un error";
break;
}
$consultasRealizadas++;
date_add($fecha, date_interval_create_from_date_string('7 days'));
$anioAuxiliar = (int) $fecha->format("Y");
}
if($consultasRealizadas == $filasAfectadas && $consultasRealizadas!=0){
$response["success"] = 1;
$response["mensaje"] = "Todo ok";
}
else {
$response["success"] = 0;
$response["mensaje"] = "Hubo un error";
}
echo json_encode($response);
?>
My database is in the same server, with phpmyadmin. Thanks for helping me!