Vérification avant insertion php - Mysql
Résolu/FerméAstolpho Messages postés 73 Date d'inscription vendredi 8 avril 2022 Statut Membre Dernière intervention 12 juin 2024 - 20 nov. 2022 à 20:13
- Vérification avant insertion php - Mysql
- Insertion liste déroulante excel - Guide
- Insertion signature word - Guide
- Easy php - Télécharger - Divers Web & Internet
- Insertion sommaire word - Guide
- Insertion filigrane word - Guide
6 réponses
19 nov. 2022 à 16:10
Bonjour
Ta question concerne davantage les requêtes SQL...
Et pour pouvoir te répondre il faudrait un minima nous montrer la structure de tes tables voire même nous fournir quelques exemples de données qui s'y trouvent.
19 nov. 2022 à 16:22
La table state :
id_state
receipt (num de quittance)
state (1 ou 2) quand la quittance est à '0' le statut se change, et la quittance va sur la page des quittance acquittées
month (mois en cours)
year
La table receipt :
id_receipt
receipt (num de quittance)
id_customers (id qui provient de la table customers)
room_number (id qui provient de la table rooms)
La table customers :
id_customers
prenom (+ infos du client)
La table rooms :
id_rooms
id_customers
room_number (+ infos de la chambre, surface, prix etc...)
19 nov. 2022 à 16:32
Tu n'as pas donné d'exemple de données... donc je ne peux qu'imaginer le fonctionnement
Donc..
Avec une requête de ce genre, tu pourras vérifier si une quittance existe en fonction des paramètres voulus (mois, id_customers et numéro de chambre )
SELECT * FROM `state` S LEFT JOIN receipt R ON R.receipt = S.receipt WHERE id_customers = '$id_customers' AND room_number = '$roomNumber' AND S.month = '$mois'
20 nov. 2022 à 15:00
J'ai les messages d'erreur ci-dessous :
( ! ) Notice: Undefined variable: roomNumber in C:\wamp64\www\24\app\process\receipts.php on line 110
Notice: Undefined variable: mois in C:\wamp64\www\24\app\process\receipts.php on line 110
( ! ) Notice: Undefined variable: roomNumber in C:\wamp64\www\24\app\process\receipts.php on line 115
Notice: Undefined variable: mois in C:\wamp64\www\24\app\process\receipts.php on line 116
<?php if(session_status() == PHP_SESSION_NONE){ session_start(); } /*Avant l'insertion de la quittance, vérifier si la quittance n'est pas déjà ouverte pour ce locataire pour le mois courant. */ //---------------------------------------------------------------// //connexion à la bdd // A lire et à appliquer avant de poursuivre : // http://www.commentcamarche.net/faq/46512-pdo-gerer-les-erreurs //---------------------------------------------------------------// require_once('../inc/connexion.php'); /** * Fonction pour créer une quittance */ function creaReceipt($receipt, $id_customers, $room_number, $id_state, $creation_date, $month, $year, $next_new_invoice ){ $db = getDB(); $id = $_SESSION['auth']->id; $stmt = $db->query(" SELECT next_new_invoice FROM users WHERE ".$_SESSION['auth']->id." = id", PDO::PARAM_STR); $stmt->bindValue("id", $id, PDO::PARAM_INT); $donnees = $stmt->fetch(); //echo $donnees['next_new_invoice']; $id_invoice = $donnees['next_new_invoice']; //$id_invoice++; //echo $id_invoice; $id_invoice++; echo '<br>'; // echo $id_invoice; $receipt=$id_invoice; $stmt = $db->prepare("INSERT INTO receipt (receipt, id_customers, room_number) VALUES(:receipt, :id_customers, :room_number)"); $stmt->bindParam("id_customers", $id_customers,PDO::PARAM_INT); $stmt->bindParam("receipt", $receipt,PDO::PARAM_INT); $stmt->bindParam("room_number", $room_number,PDO::PARAM_INT); $stmt->execute(); $receipt=$id_invoice; $stmt = $db->prepare("INSERT INTO state (id_state, receipt, creation_date, month, year) VALUES(:id_state, :receipt , :creation_date, :month, :year )"); $stmt->bindParam("id_state", $id_state,PDO::PARAM_INT); $stmt->bindParam("receipt", $receipt,PDO::PARAM_INT); $stmt->bindParam(':creation_date', $creation_date,PDO::PARAM_STR); $stmt->bindParam(':month', $month,PDO::PARAM_STR); $stmt->bindParam(':year', $year,PDO::PARAM_STR); $stmt->execute(); //mise à jour du prochain numéro de quittance $next_new_invoice=$id_invoice; //echo $next_new_invoice; $stmt = $db->prepare("UPDATE users SET next_new_invoice='$next_new_invoice' WHERE ".$_SESSION['auth']->id." = id" ); $stmt->bindParam('next_new_invoice', $_GET['next_new_invoice'], PDO::PARAM_INT); $stmt->bindValue("id", $id, PDO::PARAM_INT); $stmt->execute(); } //---------------------------------------------------// // On récupère proprement les variables envoyées // en $_POST AVANT de les utiliser // pour cela, j'utilise l'écriture ternaire : https://blog.smarchal.com/operateur-ternaire-php#:~:text=L'op%C3%A9rateur%20ternaire%20est%20un,%5BTHEN%5D%20%3A%20%5BELSE%5D%3B //---------------------------------------------------// $receipt = !empty($_POST["receipt"]) ? $_POST["receipt"] : NULL; $id_customers = !empty($_POST["id_customers"]) ? $_POST["id_customers"] : NULL; $room_number = !empty($_POST["room_number"]) ? $_POST["room_number"] : NULL; $id_state = !empty($_POST["id_state"]) ? $_POST["id_state"] : NULL; $creation_date = !empty($_POST["creation_date"]) ? $_POST["creation_date"] : NULL; $month = !empty($_POST["month"]) ? $_POST["month"] : NULL; $year = !empty($_POST["year"]) ? $_POST["year"] : NULL; $next_new_invoice = !empty($_POST["next_new_invoice"]) ? $_POST["next_new_invoice"] : NULL; $createReceipt = isset($_POST["createReceipt"]) ? $_POST["createReceipt"] : NULL; //---------------------------------------------------// // Traitement du formulaire //---------------------------------------------------// if(!empty($_POST)) { $errors =array(); $db = getDB(); $stmt = $db->query("SELECT * FROM `state` S LEFT JOIN receipt R ON R.receipt = S.receipt WHERE id_customers = '$id_customers' AND room_number = '$roomNumber' AND S.month = '$mois' "); $donnees = $stmt->fetch(); echo $id_customers ; echo '<br'; echo $roomNumber ; echo '<br'; echo $mois ; // Si il n'y a pas d'erreurs ... if(empty($errors)) { if(isset($createReceipt)){ //On crée la quittance si toutes les conditions sont réunies creaReceipt($receipt, $id_customers, $room_number, $id_state, $creation_date, $month, $year, $next_new_invoice ); // header('Location: ../array/receipts.php'); // echo "<script type='text/javascript'>document.location.replace('../array/receipts.php');</script>"; exit; //toujours mettre un exit après une redirection } } }
Modifié le 20 nov. 2022 à 19:00
J'ai bricoler ce code j'ai ajouter le code de la ligne 28 à 54, ça fonctionne, sauf que 'ai pas le message qui dit que la quiitance pour ce loctaire pour ce mois-ci est déja ouverte. (je l'ai seulement quand je désactive la redirection), avec la redirection il ne la crée pas mais il n'y à pas de message d'erreur.
<?php if(session_status() == PHP_SESSION_NONE){ session_start(); } /*Avant l'insertion de la quittance, vérifier si la quittance n'est pas déjà ouverte pour ce locataire pour le mois courant. */ //---------------------------------------------------------------// //connexion à la bdd // A lire et à appliquer avant de poursuivre : // http://www.commentcamarche.net/faq/46512-pdo-gerer-les-erreurs //---------------------------------------------------------------// require_once('../inc/connexion.php'); /** * Fonction pour créer une quittance */ function creaReceipt($receipt, $id_customers, $room_number, $id_state, $creation_date, $month, $year, $next_new_invoice ){ $db = getDB(); $stmt = $db->query(" SELECT receipt.id_customers, showin, month from receipt LEFT JOIN customers ON (receipt.id_customers=customers.id_customers) WHERE receipt.id_customers = '$id_customers' AND month=$month"); $donnees = $stmt->fetch(); $today = date("m"); echo $today; echo '<br>'; if (isset($donnees['id_customers']) == $id_customers && $month == $today OR ($donnees['month']) == $month) { echo ' <section > <div class="alert alert-danger"> <ul> la quittance pour <b>'.$donnees['showin'].' </b> est deja ouverte pour ce mois-ci. </ul> </div> </section> '; } else { $id = $_SESSION['auth']->id; $stmt = $db->query(" SELECT next_new_invoice FROM users WHERE ".$_SESSION['auth']->id." = id", PDO::PARAM_STR); $stmt->bindValue("id", $id, PDO::PARAM_INT); $donnees = $stmt->fetch(); //echo $donnees['next_new_invoice']; $id_invoice = $donnees['next_new_invoice']; //$id_invoice++; //echo $id_invoice; $id_invoice++; echo '<br>'; // echo $id_invoice; $receipt=$id_invoice; $stmt = $db->prepare("INSERT INTO receipt (receipt, id_customers, room_number, month, year) VALUES(:receipt, :id_customers, :room_number , :month, :year)"); $stmt->bindParam("id_customers", $id_customers,PDO::PARAM_INT); $stmt->bindParam("receipt", $receipt,PDO::PARAM_INT); $stmt->bindParam("room_number", $room_number,PDO::PARAM_INT); $stmt->bindParam(':month', $month,PDO::PARAM_STR); $stmt->bindParam(':year', $year,PDO::PARAM_STR); $stmt->execute(); $receipt=$id_invoice; $stmt = $db->prepare("INSERT INTO state (id_state, receipt, creation_date) VALUES(:id_state, :receipt , :creation_date )"); $stmt->bindParam("id_state", $id_state,PDO::PARAM_INT); $stmt->bindParam("receipt", $receipt,PDO::PARAM_INT); $stmt->bindParam(':creation_date', $creation_date,PDO::PARAM_STR); $stmt->execute(); //mise à jour du prochain numéro de quittance $next_new_invoice=$id_invoice; //echo $next_new_invoice; $stmt = $db->prepare("UPDATE users SET next_new_invoice='$next_new_invoice' WHERE ".$_SESSION['auth']->id." = id" ); $stmt->bindParam('next_new_invoice', $_GET['next_new_invoice'], PDO::PARAM_INT); $stmt->bindValue("id", $id, PDO::PARAM_INT); $stmt->execute(); } } //---------------------------------------------------// // On récupère proprement les variables envoyées // en $_POST AVANT de les utiliser // pour cela, j'utilise l'écriture ternaire : https://blog.smarchal.com/operateur-ternaire-php#:~:text=L'op%C3%A9rateur%20ternaire%20est%20un,%5BTHEN%5D%20%3A%20%5BELSE%5D%3B //---------------------------------------------------// $receipt = !empty($_POST["receipt"]) ? $_POST["receipt"] : NULL; $id_customers = !empty($_POST["id_customers"]) ? $_POST["id_customers"] : NULL; $room_number = !empty($_POST["room_number"]) ? $_POST["room_number"] : NULL; $id_state = !empty($_POST["id_state"]) ? $_POST["id_state"] : NULL; $creation_date = !empty($_POST["creation_date"]) ? $_POST["creation_date"] : NULL; $month = !empty($_POST["month"]) ? $_POST["month"] : NULL; $year = !empty($_POST["year"]) ? $_POST["year"] : NULL; $next_new_invoice = !empty($_POST["next_new_invoice"]) ? $_POST["next_new_invoice"] : NULL; $createReceipt = isset($_POST["createReceipt"]) ? $_POST["createReceipt"] : NULL; //---------------------------------------------------// // Traitement du formulaire //---------------------------------------------------// if(!empty($_POST)) { $errors =array(); // Si il n'y a pas d'erreurs ... if(empty($errors)) { if(isset($createReceipt)){ //On crée la quittance si toutes les conditions sont réunies creaReceipt($receipt, $id_customers, $room_number, $id_state, $creation_date, $month, $year, $next_new_invoice ); // header('Location: ../array/receipts.php'); echo "<script type='text/javascript'>document.location.replace('../array/receipts.php');</script>"; exit; //toujours mettre un exit après une redirection } } }
19 nov. 2022 à 18:28
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question20 nov. 2022 à 14:24
J'ai les messages d'erreur ci-dessous :
( ! ) Notice: Undefined variable: roomNumber in C:\wamp64\www\24\app\process\receipts.php on line 110
Notice: Undefined variable: mois in C:\wamp64\www\24\app\process\receipts.php on line 110
( ! ) Notice: Undefined variable: roomNumber in C:\wamp64\www\24\app\process\receipts.php on line 115
Notice: Undefined variable: mois in C:\wamp64\www\24\app\process\receipts.php on line 116
<?php if(session_status() == PHP_SESSION_NONE){ session_start(); } /*Avant l'insertion de la quittance, vérifier si la quittance n'est pas déjà ouverte pour ce locataire pour le mois courant. */ //---------------------------------------------------------------// //connexion à la bdd // A lire et à appliquer avant de poursuivre : // http://www.commentcamarche.net/faq/46512-pdo-gerer-les-erreurs //---------------------------------------------------------------// require_once('../inc/connexion.php'); /** * Fonction pour créer une quittance */ function creaReceipt($receipt, $id_customers, $room_number, $id_state, $creation_date, $month, $year, $next_new_invoice ){ $db = getDB(); $id = $_SESSION['auth']->id; $stmt = $db->query(" SELECT next_new_invoice FROM users WHERE ".$_SESSION['auth']->id." = id", PDO::PARAM_STR); $stmt->bindValue("id", $id, PDO::PARAM_INT); $donnees = $stmt->fetch(); //echo $donnees['next_new_invoice']; $id_invoice = $donnees['next_new_invoice']; //$id_invoice++; //echo $id_invoice; $id_invoice++; echo '<br>'; // echo $id_invoice; $receipt=$id_invoice; $stmt = $db->prepare("INSERT INTO receipt (receipt, id_customers, room_number) VALUES(:receipt, :id_customers, :room_number)"); $stmt->bindParam("id_customers", $id_customers,PDO::PARAM_INT); $stmt->bindParam("receipt", $receipt,PDO::PARAM_INT); $stmt->bindParam("room_number", $room_number,PDO::PARAM_INT); $stmt->execute(); $receipt=$id_invoice; $stmt = $db->prepare("INSERT INTO state (id_state, receipt, creation_date, month, year) VALUES(:id_state, :receipt , :creation_date, :month, :year )"); $stmt->bindParam("id_state", $id_state,PDO::PARAM_INT); $stmt->bindParam("receipt", $receipt,PDO::PARAM_INT); $stmt->bindParam(':creation_date', $creation_date,PDO::PARAM_STR); $stmt->bindParam(':month', $month,PDO::PARAM_STR); $stmt->bindParam(':year', $year,PDO::PARAM_STR); $stmt->execute(); //mise à jour du prochain numéro de quittance $next_new_invoice=$id_invoice; //echo $next_new_invoice; $stmt = $db->prepare("UPDATE users SET next_new_invoice='$next_new_invoice' WHERE ".$_SESSION['auth']->id." = id" ); $stmt->bindParam('next_new_invoice', $_GET['next_new_invoice'], PDO::PARAM_INT); $stmt->bindValue("id", $id, PDO::PARAM_INT); $stmt->execute(); } //---------------------------------------------------// // On récupère proprement les variables envoyées // en $_POST AVANT de les utiliser // pour cela, j'utilise l'écriture ternaire : https://blog.smarchal.com/operateur-ternaire-php#:~:text=L'op%C3%A9rateur%20ternaire%20est%20un,%5BTHEN%5D%20%3A%20%5BELSE%5D%3B //---------------------------------------------------// $receipt = !empty($_POST["receipt"]) ? $_POST["receipt"] : NULL; $id_customers = !empty($_POST["id_customers"]) ? $_POST["id_customers"] : NULL; $room_number = !empty($_POST["room_number"]) ? $_POST["room_number"] : NULL; $id_state = !empty($_POST["id_state"]) ? $_POST["id_state"] : NULL; $creation_date = !empty($_POST["creation_date"]) ? $_POST["creation_date"] : NULL; $month = !empty($_POST["month"]) ? $_POST["month"] : NULL; $year = !empty($_POST["year"]) ? $_POST["year"] : NULL; $next_new_invoice = !empty($_POST["next_new_invoice"]) ? $_POST["next_new_invoice"] : NULL; $createReceipt = isset($_POST["createReceipt"]) ? $_POST["createReceipt"] : NULL; //---------------------------------------------------// // Traitement du formulaire //---------------------------------------------------// if(!empty($_POST)) { $errors =array(); $db = getDB(); $stmt = $db->query("SELECT * FROM `state` S LEFT JOIN receipt R ON R.receipt = S.receipt WHERE id_customers = '$id_customers' AND room_number = '$roomNumber' AND S.month = '$mois' "); $donnees = $stmt->fetch(); echo $id_customers ; echo '<br'; echo $roomNumber ; echo '<br'; echo $mois ; // Si il n'y a pas d'erreurs ... if(empty($errors)) { if(isset($createReceipt)){ //On crée la quittance si toutes les conditions sont réunies creaReceipt($receipt, $id_customers, $room_number, $id_state, $creation_date, $month, $year, $next_new_invoice ); // header('Location: ../array/receipts.php'); // echo "<script type='text/javascript'>document.location.replace('../array/receipts.php');</script>"; exit; //toujours mettre un exit après une redirection } } }
20 nov. 2022 à 19:28
<?php if(session_status() == PHP_SESSION_NONE){ session_start(); } /*Avant l'insertion de la quittance, vérifier si la quittance n'est pas déjà ouverte pour ce locataire pour le mois courant. */ //---------------------------------------------------------------// //connexion à la bdd // A lire et à appliquer avant de poursuivre : // http://www.commentcamarche.net/faq/46512-pdo-gerer-les-erreurs //---------------------------------------------------------------// require_once('../inc/connexion.php'); /** * Fonction pour créer une quittance */ function creaReceipt($receipt, $id_customers, $room_number, $id_state, $creation_date, $month, $year, $next_new_invoice ){ $db = getDB(); $sql = " SELECT receipt.id_customers, showin, month FROM receipt LEFT JOIN customers ON (receipt.id_customers=customers.id_customers) WHERE receipt.id_customers = '$id_customers' AND month='$month'"; $stmt = $db->query($sql); $donnees = $stmt->fetch(); if (!empty($donnees)) { echo ' <section > <div class="alert alert-danger"> <ul> la quittance pour <b>'.$donnees['showin'].' </b> est deja ouverte pour ce mois-ci. </ul> </div> </section> '; return false; } else { $id = $_SESSION['auth']->id; $stmt = $db->query(" SELECT next_new_invoice FROM users WHERE ".$_SESSION['auth']->id." = id", PDO::PARAM_STR); $stmt->bindValue("id", $id, PDO::PARAM_INT); $donnees = $stmt->fetch(); //echo $donnees['next_new_invoice']; $id_invoice = $donnees['next_new_invoice']; //$id_invoice++; //echo $id_invoice; $id_invoice++; echo '<br>'; // echo $id_invoice; $receipt=$id_invoice; $stmt = $db->prepare("INSERT INTO receipt (receipt, id_customers, room_number, month, year) VALUES(:receipt, :id_customers, :room_number , :month, :year)"); $stmt->bindParam("id_customers", $id_customers,PDO::PARAM_INT); $stmt->bindParam("receipt", $receipt,PDO::PARAM_INT); $stmt->bindParam("room_number", $room_number,PDO::PARAM_INT); $stmt->bindParam(':month', $month,PDO::PARAM_STR); $stmt->bindParam(':year', $year,PDO::PARAM_STR); $stmt->execute(); $receipt=$id_invoice; $stmt = $db->prepare("INSERT INTO state (id_state, receipt, creation_date) VALUES(:id_state, :receipt , :creation_date )"); $stmt->bindParam("id_state", $id_state,PDO::PARAM_INT); $stmt->bindParam("receipt", $receipt,PDO::PARAM_INT); $stmt->bindParam(':creation_date', $creation_date,PDO::PARAM_STR); $stmt->execute(); //mise à jour du prochain numéro de quittance $next_new_invoice=$id_invoice; //echo $next_new_invoice; $stmt = $db->prepare("UPDATE users SET next_new_invoice='$next_new_invoice' WHERE ".$_SESSION['auth']->id." = id" ); $stmt->bindParam('next_new_invoice', $_GET['next_new_invoice'], PDO::PARAM_INT); $stmt->bindValue("id", $id, PDO::PARAM_INT); $stmt->execute(); return true; } } //---------------------------------------------------// // On récupère proprement les variables envoyées // en $_POST AVANT de les utiliser // pour cela, j'utilise l'écriture ternaire : https://blog.smarchal.com/operateur-ternaire-php#:~:text=L'op%C3%A9rateur%20ternaire%20est%20un,%5BTHEN%5D%20%3A%20%5BELSE%5D%3B //---------------------------------------------------// $receipt = !empty($_POST["receipt"]) ? $_POST["receipt"] : NULL; $id_customers = !empty($_POST["id_customers"]) ? $_POST["id_customers"] : NULL; $room_number = !empty($_POST["room_number"]) ? $_POST["room_number"] : NULL; $id_state = !empty($_POST["id_state"]) ? $_POST["id_state"] : NULL; $creation_date = !empty($_POST["creation_date"]) ? $_POST["creation_date"] : NULL; $month = !empty($_POST["month"]) ? $_POST["month"] : NULL; $year = !empty($_POST["year"]) ? $_POST["year"] : NULL; $next_new_invoice = !empty($_POST["next_new_invoice"]) ? $_POST["next_new_invoice"] : NULL; $createReceipt = isset($_POST["createReceipt"]) ? $_POST["createReceipt"] : NULL; //---------------------------------------------------// // Traitement du formulaire //---------------------------------------------------// if(!empty($_POST)) { $errors =array(); // Si il n'y a pas d'erreurs ... if(empty($errors)) { if(isset($createReceipt)){ //On crée la quittance si toutes les conditions sont réunies $result = creaReceipt($receipt, $id_customers, $room_number, $id_state, $creation_date, $month, $year, $next_new_invoice ); if( $result == true ){ // header('Location: ../array/receipts.php'); echo "<script type='text/javascript'>document.location.replace('../array/receipts.php');</script>"; exit; //toujours mettre un exit après une redirection } } } }
20 nov. 2022 à 20:13
Merci, ça fonctionne parfaitment.
Bonne soirée.