Ich erstelle eine PHP, die mehrere Abfragen ausführen. Ich möchte eine Transaktion machen, weil ich die gesamte Abfrage speichern muss, wenn alles in Ordnung ist, wenn nicht ein Rollback. Ist kritisch, speichern Sie alle Daten. Ich weiß nicht, wie ich die Transaktion machen soll. Ich habe das verfolgt: PHP + MySQL transactions examples aber es funktioniert nicht.Transaktion in PHP mit MySQL?
Das ist mein 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);
?>
Meine Datenbank in demselben Server befindet, mit phpMyAdmin. Danke, dass du mir geholfen hast!
[Little Bobby] (http://bobby-tables.com/) sagt [Ihr Skript ist für SQL Injection Attacks gefährdet.] (Http://stackoverflow.com/questions/60174/how-can-i -prevent-sql-injection-in-php). Sogar [die Zeichenfolge zu entkommen] (http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) ist nicht sicher! –
Bitte [stoppen Sie mit mysql_ * 'Funktionen] (http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [Diese Erweiterungen] (http://php.net/manual/en/migration70.removed-exts-sapis.php) wurden in PHP 7 entfernt. Erfahren Sie mehr über [prepared] (http: // en.wikipedia.org/wiki/Prepared_statement) Anweisungen für [PDO] (http://php.net/manual/en/pdo.prepared-statements.php) und [MySQLi] (http://php.net/manual/en /mysqli.quickstart.prepared-statements.php) und erwäge die Verwendung von PDO, [es ist wirklich ziemlich einfach] (http://jayblanchard.net/demystifying_php_pdo.html). –
Welchen Tabellentyp verwenden Sie? Ist es InnoDB? –