Regarde la différence ...
et :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 <?php include('fonctions.php'); // connexion a la BdD connect(); ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" > <head> <link rel="stylesheet" media="screen" type="text/css" title="Design" href="budget_css.css" /> <title>Modifier une operations</title> </head> <body > <div id="formulaire"> <!-- formulaire --> <form name="test" action="modif_echo_op.php" method="post" > <fieldset ><legend><b>Compte à modifier</b></legend> <table border="1" style="border-collapse:collapse" align="center" width="60%"> <thead> <tr> <th>Numéro compte</th> <th>Libelle</th> <th>Code_cr</th> <th>Montant</th> <th>Date</th> <th>reference</th> <th>fournisseur</th> <th>A modifier</th> </tr> </thead> <tbody> <?php // requete : operations $sql = "SELECT * FROM operations;"; $sql_result = mysql_query($sql); while($row_op_mod = mysql_fetch_array($sql_result)) { ?> <tr> <td width="10%"> <input type="text" name="Num_compte[]" size="15" value="<?php echo $row_op_mod['Num_compte']; ?>" /> </td> <td> <input type="text" name="Libelle[]" size="40" value="<?php echo $row_op_mod['libelle']; ?>" /> </td> <td class="cr"> <input type="text" name="Code_cr[]" size="15" value="<?php echo $row_op_mod['Code_cr']; ?>" /> </td> <td width="10%"> <input type="text" name="Montant[]" size="15" value="<?php echo $row_op_mod['montant_realise']; ?>" /> </td> <td width="10%"> <input type="text" name="Date[]" size="10" value="<?php echo $row_op_mod['date_realise']; ?>" /> </td> <td> <input type="text" name="Reference[]" value="<?php echo $row_op_mod['reference']; ?>" /> </td> <td> <input type="text" name="Fournisseur[]" value="<?php echo $row_op_mod['fournisseur']; ?>" /> </td> <td> <input type="checkbox" name="labox[]" value="ON" /> <input type="hidden" name="ID[]" value="<?php echo $row_op_mod['id_operation']; ?>" /> </td> </tr> <?php } // fin while ?> <tr align="center"> <td colspan="3"> <input type="submit" name="envoiform" value="Modifier les données" /> </td> </tr> </tbody> </table> </fieldset> </form> </div> </body> </html>
Un code "propre" FACILITE grandement la lecture du fichier, et son débugage ....
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 <?php include('fonctions.php'); // connexion a la BdD connect(); // LA PARTIE CI-DESSUS EST INUTILE SI le fichier est en INCLUDE dans l'autre ! ?> <?php $nb_modif = 0; if(isset($_POST['envoiform'])) // (si le formulaire a été envoyé) { if (isset($_POST['labox'])) { // recuperation des checkbox (array !) $array_box = $_POST['labox']; print_r($array_box); // on parcours l'array foreach($array_box as $key => $val) { // Si LA CASE EST COCHEE -> on traite les donnees if($val =='ON') { // on recupere les donnees (de la ligne correspondante) $ID = $_POST['ID'][$key]; $num_compte = $_POST['Num_compte'][$key]; $libelle = $_POST['Libelle'][$key]; $code_cr = $_POST['Code_cr'][$key]; $montant = $_POST['Montant'][$key]; $date_realise = $_POST['Date'][$key]; $reference = $_POST['Reference'][$key]; $fournisseur = $_POST['Fournisseur'][$key]; // on protège la BdD contre les injections SQL $ID = mysql_real_escape_string($ID); $num_compte = mysql_real_escape_string($num_compte); $libelle = mysql_real_escape_string($libelle); $code_cr = mysql_real_escape_string($code_cr); $montant = mysql_real_escape_string($montant); $date_realise = mysql_real_escape_string($date_realise); $reference = mysql_real_escape_string($reference); $fournisseur = mysql_real_escape_string($fournisseur); // on modifie l'enregistrement en BdD $query_update = "UPDATE operations SET Num_compte = '".$num_compte."', libelle = '".$libelle."', code_cr = '".$code_cr."', montant_realise = '"$montant.", date_realise = '".$date_realise."', reference = '".$reference."', fournisseur = '".$fournisseur."' WHERE id_operation = '".$ID."';"; $res_query = mysql_query($query_update) or die('Erreur SQL :<br />'.$query_update.'<br />'.mysql_error()); if ($res_query !== false){ $nb_modif = $nb_modif+1; } } // fin si case cochée } // fin foreach } // fin if labox unset($POST); echo 'Vous avez modifié '.$nb_modif.' lignes'; }// fin si le formulaire envoyé ?>
Partager