1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| procedure TFStock.BtnAjouterClick(Sender: TObject);
var
nom_produit: String;
meme_produit: Integer;
begin
//boite de dialogue avec entrée de texte
repeat
nom_produit := InputBox ('Enregistrer un nouveau produit',
'Entrer le nom du produit:', '');
//vérifier qu'il n'est pas encore enregistré
SQLQuery1.Close;
SQLQuery1.SQL.Text:= 'SELECT COUNT(pro_nom) AS count_nom FROM produit WHERE pro_nom = '''+nom_produit+''';';
SQLQuery1.Open;
meme_produit:= SQLQuery1.Fields[0].AsInteger;
if nom_produit = '' then
begin
ShowMessage('Le champ est vide.');
Exit;
end
else if meme_produit > 0 then //il exite deja
begin
if MessageDlg ('Error', 'Erreur: un autre produit porte le même nom!', mtConfirmation,
[mbOK, mbCancel],0) = mrCancel then Exit;
end
until (meme_produit = 0) and (nom_produit <> '');
//Vérification que l'utilisateur n'a rien écrit
FMenu.SQLQuery1.Close; //Enregistrement
FMenu.SQLQuery1.SQL.Text:= 'INSERT INTO `produit` (`pro_id`, `pro_nom`, `pro_quantite`, `pro_date_maj`) '
+'VALUES (NULL, :pro_nom , 0, CURRENT_TIMESTAMP);';
FMenu.SQLQuery1.Prepare;
FMenu.SQLQuery1.Params.ParamByName('pro_nom').AsString := nom_produit;
FMenu.SQLQuery1.ExecSQL;
FMenu.SQLTransaction1.Commit;
FMenu.SQLTransaction1.StartTransaction;
AffichStock;
ShowMessage('Le produit a bien été enregistré.');
end; |
Partager