Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD > PHP & MySQL
PHP & MySQL Forum d'entraide sur les fonctions MySQL avec PHP. Avant de poster -> FAQ MySQL, Cours MySQL et Sources MySQL. Pour les questions concernant le moteur MySQL plutôt que les fonctions PHP, merci d'utiliser le forum MySQL.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 23/05/2011, 14h50   #1
Futur Membre du Club
 
Inscription : décembre 2010
Messages : 74
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 74
Points : 15
Points : 15
Par défaut classe php pour faire un update table mysql

Hello tout le monde,

Je reviens un peu sur cette classe que Sabotage m'avais modifié afin de rendre les Id alphanumérique possibles.

A l'heure actuelle cette classe permet d'ajouter et de supprimer de manière unidirectionnelle des données. (selon ajout ou suppression des ID)

Je souhaiterai la modifier afin de non plus supprimer mais faire un update de la table.
J'ai commencé à modifier les lignes de code mais dans mon cas si je veux modifier dans ma table le nom d'une rue, il ne prend pas en compte la modif car l'ID reste identique.

Pourriez-vous me dire ce qu'il faut modifier dans ce cas ?

Merci beaucoup.

Voici la classe:

Code :
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
    class DB
    {
        //Member Zone
        var $dataserver;
        var $database;
        var $result;
        var $row_result;
        var $error_name;
        var $error_number;
 
 
        //Methode Zone
        function DB()
        {
        }
 
        function DBDown()
        //database down
        {
            $this->error_number = mysql_errno();
            $this->error_name = mysql_error(); 
 
 
            if ($this->error_name != "")
            {
                print '<p></p>';
				print '<form name="formulaire" method="post" action="../index.php">
				<input type="submit" value="Ok"  style="height:50px; width:138px"/>
				</form>';
            }
            exit();
        }
 
        function Open($plocation,$puser,$ppassword,$pdb)
        //Open a connection to a MySQL Server
        {
            $this->database = $pdb;
            if(!$this->dataserver = mysql_pconnect($plocation, $puser, $ppassword))
            {
                DB::DBDown();
            }
        }
 
        function executeSQL($sql)
        //Query database
        {
            if (!$res=mysql_query($sql,$this->dataserver))
            {
                DB::DBDown();
            }
            $this->row_result=0;
            $this->result=array();
            //print "sql=".$sql."<br>";
            if ((strpos("_".$sql,'SELECT'))||(strpos("_".$sql,'SHOW'))||(strpos("_".$sql,'DESCRIBE'))||(strpos("_".$sql,'EXPLAIN')))
            {
                while ($this->result[]=mysql_fetch_array($res, MYSQL_NUM))
                {
                    $this->row_result++;
                }
            }
        }
 
        function cleanSQL($param)
        //Clean sql parameters
        {
            return mysql_real_escape_string($param,$this->dataserver);
        }
 
    }
 
    class SyncronizeDB
    {
        //Member Zone
        var $db_master;
        var $table_master;
        var $idx_master;
        var $col_master;
        var $db_slave;
        var $table_slave;
        var $idx_slave;
        var $col_slave;
 
        //Methode Zone
        function SyncronizeDB()
        //HelpdeskDB Constructor
        {
        }
 
 
        function masterColumns()
        {
            //return columns name
            $this->db_master->executeSQL("SHOW COLUMNS from ".$this->db_master->database.".".$this->table_master);
            $i = 0;
            $col = "";
            foreach ($this->db_master->result as $row)
            {
                if ($row != "")
                {            
                    if ($i == 1) $col .= ",";
                    $col .= $row[0];
                    if ($i == 0) $i = 1;
                }
            }
            return $col;
        }
 
        function masterAllIndexes()
        {
            //return all index value
            $this->db_master->executeSQL("SELECT ".$this->idx_master." from ".$this->db_master->database.".".$this->table_master);
        }
 
        function masterDataRows($sin)
        {
            // return data from master database
            $this->db_master->executeSQL("SELECT * from ".$this->db_master->database.".".$this->table_master." where ".$this->idx_master." in (".$sin.")");
        }
 
        function masterSet($mplocation, $mpuser, $mppassword, $mpdb, $mptable, $mpidx)
        //set master database
        {
            $this->table_master = $mptable;
            $this->idx_master = $mpidx;
            $this->db_master = new DB();
            $this->db_master->Open($mplocation,$mpuser,$mppassword,$mpdb);    
            $this->col_master = $this->masterColumns();
        }
 
        function slaveColumns()
        {
            //return columns name
            $this->db_slave->executeSQL("SHOW COLUMNS from ".$this->db_slave->database.".".$this->table_slave);
            $i = 0;
            $col = "";
            foreach ($this->db_slave->result as $row)
            {
                if ($row != "")
                {
                    if ($i == 1) $col .= ",";
                    $col .= $row[0];
                    if ($i == 0) $i = 1;
                }
            }
            return $col;
        }
 
        function slaveAllIndexes()
        {
            //return all index value
            $this->db_slave->executeSQL("SELECT ".$this->idx_slave." from ".$this->db_slave->database.".".$this->table_slave);
        }
 
        function slaveSet($mplocation, $mpuser, $mppassword, $mpdb, $mptable, $mpidx)
        //set slave database
        {
            $this->table_slave = $mptable;
            $this->idx_slave = $mpidx;            
            $this->db_slave = new DB();
            $this->db_slave->Open($mplocation,$mpuser,$mppassword,$mpdb);    
            $this->col_slave = $this->slaveColumns();            
        }    
 
        function slaveSyncronization()
        {
            $this->masterAllIndexes();
            $masterid=array();
            foreach ($this->db_master->result as $id)
            {
                $masterid[] = $id[0];
            }            
 
 
 
            $this->slaveAllIndexes();
            $slaveid=array();
            foreach ($this->db_slave->result as $id)
            {
                $slaveid[] = $id[0];
            }    
 
 
 
            $in = array_diff($masterid, $slaveid);
            echo "Envoi et t&eacute;l&eacute;chargement des donn&eacute;es: ";
 
 
 
			foreach($in as $value)
			{
  			$sin[] = "'" . mysql_real_escape_string($value) . "'";
			}
 
			$sin = implode(",", $sin);
 
			if ($sin == "")
 
			{
				echo "<b>aucune</b>";
                echo ".<br />";                
 
			}
 
            else
            {
                echo '<font color="#009900">';
				echo 'Succes';
				echo '</font>';
                echo ".<br />";                
                $this->masterDataRows($sin);
                $sql = "INSERT INTO ".$this->db_slave->database.".".$this->table_slave." (".$this->col_slave.") values ";
                $i = 0;
                foreach ($this->db_master->result as $row)
                {
                    if (count($row) > 1)
                    {
                        if ($i == 1) $sql .= ",";
                        $sql .= "(";
                        $j=0;
                        //print_r($row);
                        foreach ($row as $col)
                        {
                            if ($j == 1) $sql .= ",";
                            $col = $this->db_slave->cleanSQL($col);
                            $sql .= "'".$col."'";
                            if ($j == 0) $j = 1;
                        }
                        $sql .= ")";
                        if ($i == 0) $i = 1;
                    }
                }        
 
                $this->db_slave->executeSQL($sql);                
            }
 
            $out = array_diff($slaveid, $masterid);
            foreach($out as $value)
			{
  			$sout[] = "'" . mysql_real_escape_string($value) . "'";
			}
 
            $sout = implode(",", $sout);
            if ($sout == "")
            {
                echo "<b>aucune</b>";
                echo ".<br />";                
            }
            else
            {
             $sql = "UPDATE ".$this->db_slave->database.".".$this->table_slave." SET ".$this->col_slave." where ".$this->table_slave.				".".$this->idx_slave." (".$sout.") ";
 
			   echo "Commande SQL:<br />".$sql;    
                echo ".<br />"; 
                $this->db_slave->executeSQL($sql);       
 
            }
        }
    }
 
?>
legrandse est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/05/2011, 15h46   #2
Membre régulier
 
Homme Nicolas
Étudiant
Inscription : mai 2010
Messages : 308
Détails du profil
Informations personnelles :
Nom : Homme Nicolas
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : mai 2010
Messages : 308
Points : 74
Points : 74
Pour faire un update d'une ligne de ta BDD, tu dois lui passer les informations de manière implicite (avec l'attribut hidden).
Et ensuite utiliser la requête SQL UPDATE afin de modifier en SQL les informations de ta base.
feldi est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/05/2011, 15h52   #3
Futur Membre du Club
 
Inscription : décembre 2010
Messages : 74
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 74
Points : 15
Points : 15
Merci pour ta réponse.

Cependant j'ai du mal a comprendre ce que tu me dis.

Cà se traduit comment en instruction ?

Merci.
legrandse est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 15h12.


 
 
 
 
Partenaires

Hébergement Web