I did a function to record some informations in my database, some elements of the form posted are provided by a select list (multi select).
So I have an array to store in my database.
I have to use transaction in order to make sure that everything is fine, but it is the first time I use transaction, and I do not know how to proceed for the loop.
Below is my function:
function recordSports() {
if (isset($_POST['edit_sport'])) {
//echappement des variables
$liste_sport = mysql_real_escape_string($_POST['liste_sport']);
$id_user = $_SESSION['login'];
//démarrage des transactions
mysql_query("START TRANSACTION");
//requettage
$query1 = "DELETE FROM `fit__sport_practice`
WHERE
`sport_practice__id_user` = {$id_user}
";
mysql_query($query1);
foreach ($liste_sport as $sport) {
$query = "INSERT INTO `fit__sport_practice`
SET
`sport_practice__id_user` = {$id_user},
`sport_practice__id_sport` = {$sport},
";
mysql_query($query);
}
if ($query1) {
mysql_query("COMMIT");
$retour['message'] = $_SESSION['maj_sport_ok'];
$retour['error'] = 0;
} else {
//sinon on renvoit une erreur
mysql_query("ROLLBACK");
$retour['message'] = $_SESSION['maj_sport_error'];
$retour['error'] = 1;
}
} else {
$retour['message'] = null;
$retour['error'] = null;
}
return $retour;
}