複数のクエリを実行するPHPを作成します。私はすべてのクエリを保存する必要があるので、私はトランザクションをしたいと思います。重要なのはすべてのデータを保存することです。私は取引の仕方を知らない。私はそれに従ってきた:PHP + MySQL transactions examplesしかし、それは動作していません。MySQLでのPHPのトランザクション?
これは私の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);
?>
私のデータベースはphpmyadminのと、同じサーバーです。 私を助けてくれてありがとう!
[Little Bobby](http://bobby-tables.com/)は[あなたのスクリプトはSQLインジェクション攻撃の危険性があると言います。](http://stackoverflow.com/questions/60174/how-can-i -prevent-sql-injection-in-php)を実行します。 [文字列をエスケープする](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)でも安全ではありません! –
[mysql_ *関数の使用をやめてください](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [これらの拡張機能](http://php.net/manual/en/migration70.removed-exts-sapis.php)はPHP 7で削除されました。[prepared](http:// en。wikipedia.org/wiki/Prepared_statement)[PDO](http://php.net/manual/en/pdo.prepared-statements.php)と[MySQLi](http://php.net/manual/en /mysqli.quickstart.prepared-statements.php)、PDOの使用を検討してください。[これは本当に簡単です](http://jayblanchard.net/demystifying_php_pdo.html)。 –
どのテーブルタイプを使用していますか?それはInnoDBですか? –