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 18/03/2010, 09h48   #1
Invité de passage
 
marwen mlayah
Inscription : décembre 2009
Messages : 31
Détails du profil
Informations personnelles :
Nom : marwen mlayah

Informations forums :
Inscription : décembre 2009
Messages : 31
Points : 4
Points : 4
Par défaut update d'un table de bd

bonjour a tt
deux table:
produit :"id_produit"
film :"id_film"
on veut modifier les champs de ces table voila le code

affichage :
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
<?php
	$host  = 'localhost';  // serveur 
	$user  = 'root';  // identifiant
	$mdp  = '';  // mot de passe
	$base  = 'ruspina';  // nom de la base
 
	// Connexion au serveur
	mysql_connect($host, $user, $mdp)
		or die('Impossible de se connecter au serveur '.$host);
 
	// Sélection d'une base
	mysql_select_db($base)
		or die('Impossible de sélectionner la base '.$base);
 
 
$result = mysql_query("select * from produit where type=\"film\" ")
	or die ("Execution impossible de la requete");
$nb1=mysql_num_rows($result);	
 
	$result1 = mysql_query("select * from film")
	or die ("Execution impossible de la requete");
$nb2=mysql_num_rows($result1);
	?>
	<table width="169%" border="1">
  <tr>
    <td colspan="14"><div align="center">
      <p><img src="image/CATHALOGEFILM.png" width="641" height="139" /></p>
      </div></td>
  </tr>
  <tr>
   <td width="8%"><span class="Style1">ID_PRODUIT</span></td>
    <td width="4%"><span class="Style1">TITRE</span></td>
    <td width="4%"><span class="Style2">ICONE</span></td>
    <td width="6%"><span class="Style2">POSTURE</span></td>
    <td width="10%"><span class="Style2"> SYSTEME EXPLOITATION</span> </td>
    <td width="5%"><span class="Style2">ANNE&Eacute;</span></td>
    <td width="8%"><span class="Style2">REFERENCE</span></td>
    <td width="3%"><span class="Style2">PRIX</span></td>
	<td width="4%"><span class="Style2">POIDS</span></td>
    <td width="9%"><span class="Style2">REALISATEUR</span></td>
	<td width="5%"><span class="Style2">ACTEUR</span></td>
	<td width="6%"><span class="Style2">LANGUE</span></td>
	<td width="6%"><span class="Style2">ORIGINE</span></td>
	<td width="12%"><span class="Style2">DUREE</span></td>
 
  </tr>
   <?php
  if($nb1>0 || $nb2>0)
  {
  for($i=0;$i<$nb1;$i++)
  {
	$ligne = mysql_fetch_array($result);
	$ligne2 = mysql_fetch_array($result1);
 
	?>
  <tr>
    <td><div align="center"><?php echo  $ligne['id_produit'];  ?> </div></td>
    <td><div align="center"><?php echo  $ligne['titre'];  ?> </div></td>
    <td><div align="center"> <?php echo  $ligne['icone'];  ?></div></td>
    <td><div align="center"><?php echo  $ligne['posture'];  ?></div></td>
    <td><div align="center"><?php echo  $ligne['systeme_exploitation'];  ?> </div></td>
    <td><div align="center"><?php echo  $ligne['annee'];  ?> </div></td>
    <td><div align="center"><?php echo  $ligne['reference'];  ?> </div></td>
    <td><div align="center"><?php echo  $ligne['prix'];  ?> </div></td>
	 <td><div align="center"><?php echo  $ligne['poids'];  ?> </div></td>
	  <td><div align="center"><?php echo  $ligne2['realisateur'];  ?>    </div></td>
	   <td><div align="center"><?php echo  $ligne2['acteur'];  ?> </div></td>
		 <td><div align="center"><?php echo  $ligne2['langue'];  ?> </div></td> 
		<td><div align="center"><?php echo  $ligne2['origine'];  ?> </div></td> 
		 <td><div align="center"><?php echo  $ligne2['duree'];  ?> </div></td>
 
    <td width="10%"><a href="action_supprimer_film.php?pg=<?php echo $ligne[id_produit]; ?>">Supprimer</a></td> 
	<td width="10%"><a href="modifier_film.php?tyty=<?php echo $ligne[id_produit]; ?>">Modifier</a></td> 
	<?php
	}}
	?>
Code :
<a href="modifier_film.php?tyty=<?php echo $ligne[id_produit]; ?>">Modifier</a>
lien vers cette page:

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
<body >
<p class="Style1 Style3 Style5">modifier un film :</p>
<form   name="form1" id="form1" method="post" enctype="multipart/form-data"  action="action_modifier_film.php">
  <table width="52%" align="center" border="1"background="image/fontpg.jpg">
    <tr>
      <td width="23%" height="40">titre:</td>
      <td width="77%"><input name="titre" type="text"  id="titre" width="332" /></td>
    </tr>
	<tr>
	<td width="23%" height="40">type:</td>
	<td><input name="type" type="text"  id="type" width="332" /></td>
	</tr>
    <tr>
      <td width="23%" height="40">icone:</td>
      <td><label>
        <input name="icone" type="file" id="icone" />
      </label></td>
    </tr>
    <tr>
      <td width="23%" height="40">posture:</td>
      <td><label>
        <input name="posture" type="file" id="posture" />
      </label></td>
    </tr>
    <tr>
      <td width="23%" height="40">systeme exploitation : </td>
      <td><input name="se" type="text"  id="sys" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">ann&eacute;e:</td>
      <td><input name="annee" type="text"  id="ann" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">reference:</td>
      <td><input name="ref" type="text"  id="ref" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">realisateur:</td>
      <td><input name="real" type="text"  id="rea" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">acteur:</td>
      <td><input name="acteur" type="text"  id="act" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">langue:</td>
      <td><input name="lang" type="text"  id="lan" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">origine:</td>
      <td><input name="origine" type="text"  id="or" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">dur&eacute;e:</td>
      <td><input name="duree" type="text"  id="dur" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">prix:</td>
      <td><input name="prix" type="text"  id="pri" width="332" /></td>
    </tr>
	  <td width="23%" height="40">poids:</td>
	    <td><input name="poid" type="text"  id="poid" width="332" /></td>
  </table>
 
  <br />
  <p align="center">
    <label>
    <input type="submit" name="Submit" value="Modifier" />
    </label>
    <input type="reset" name="annuler" value="retablir" />
  </p>
 
</form>
 
</body>
action de cette page:
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
<?php
if(isset($_GET['tyty']))
{
 
$enregistrer_sous1 = 'D:\MARWEN\TRI 3\videotheque\ruspina\images'; 
$enregistrer_sous2 = 'D:\MARWEN\TRI 3\videotheque\ruspina\images'; 
 
$tmp_fichier1 = $_FILES['icone']['tmp_name'];
	$type_fichier1 = $_FILES['icone']['type'];
	 $nom_fichier1 = $_FILES['icone']['name'];
 
	 $tmp_fichier2 = $_FILES['posture']['tmp_name'];
	$type_fichier2 = $_FILES['posture']['type'];
	 $nom_fichier2 = $_FILES['posture']['name'];
 
move_uploaded_file($tmp_fichier1, $enregistrer_sous1 . $nom_fichier1);
move_uploaded_file($tmp_fichier2, $enregistrer_sous2 . $nom_fichier2);
 
 
$connexion=mysql_connect('localhost','root','') or die ("connexion impossible.");
$db=mysql_select_db('ruspina',$connexion) or die ("base de données non accessible");
 
 
$a= $_POST['titre'];
$z=$_FILES['icone']['name'];
$e=$_FILES['posture']['name'];
$r=$_POST['se'];
$t=$_POST['annee'];
$y=$_POST['ref'];
$u=$_POST['real'];
$o=$_POST['acteur'];
$p=$_POST['lang'];
$q=$_POST['origine'];
$s=$_POST['duree'];
$d=$_POST['prix'];
$f=$_POST['poid'];
$g=$_POST['type'];
 
 
$id=$_GET['tyty'];
$req="update produit set id_produit='$id',type='$g',titre='$a',icone='$z',posture='$e', systeme_exploitation='$r',annee='$t',reference='$y',prix='$d',poids='$f'";
$res=mysql_query($req);
if(!$res)
{
echo"insertion d'un nouveau element evec succe";
}
else
{
echo"probleme";
}
}
?>
bon la probleme je veux quand je clique sur modifier d'un ligne, les données seront affiché dans la page de modification et aprés avoir le modifié elle sera affiché dans premier table(resultat).je crois que mon probleme est au niveau de memoriser id_produit quand je veux modifier un ligne plus la probleme de session
svp aider moi
je vous remercie
marwen86 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/03/2010, 10h29   #2
Membre habitué
 
Inscription : juillet 2009
Messages : 156
Détails du profil
Informations forums :
Inscription : juillet 2009
Messages : 156
Points : 133
Points : 133
tu fais un update de ta BDD mais tu oublie la chose la plus importante dans ta requête... dans quel cas il doit faire cet update???

Généralement, dans une bdd pour chaque table, chaque entrée à un id unique (si on fait ca bien...)
Au moment de modifier une ligne de ta table, tu récupère l'id unique de ton objet... tu le stock... par exemple dans ton formulaire et un input hidden.

Ensuite tu fais ta requete avec cet id la...
un exemple de mes ligne d'update:
Code :
1
2
$sql = "UPDATE `news` SET titre='%s', lien='%s', type='%s', ordre='%d', lang='%s', source='%s' WHERE id='%d'";
$sql = sprintf($sql, FormatSQL($titre), FormatSQL($lien), FormatSQL($type), FormatSQL($ordre), FormatSQL($lang), FormatSQL($source), FormatSQL($id));
FormatSQL = une fonction a moi qui sécurise mes entrées pour les envoyer ensuite à mysql entre autre "mysql_real_escape_string" et autres

Donc 2 choses dans ton cas, tes entrées SQL ne sont pas protégé (Injection SQL facile) et il te manque ta condition "WHERE" sur l'update...
Ze AzAr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/03/2010, 11h44   #3
Invité de passage
 
marwen mlayah
Inscription : décembre 2009
Messages : 31
Détails du profil
Informations personnelles :
Nom : marwen mlayah

Informations forums :
Inscription : décembre 2009
Messages : 31
Points : 4
Points : 4
input hidden ??
svp je suis débutant essayer de plus detailler
svp
merci
marwen86 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/03/2010, 11h51   #4
Membre habitué
 
Inscription : juillet 2009
Messages : 156
Détails du profil
Informations forums :
Inscription : juillet 2009
Messages : 156
Points : 133
Points : 133
quel est le id unique de ta table???
Ze AzAr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/03/2010, 11h54   #5
Invité de passage
 
marwen mlayah
Inscription : décembre 2009
Messages : 31
Détails du profil
Informations personnelles :
Nom : marwen mlayah

Informations forums :
Inscription : décembre 2009
Messages : 31
Points : 4
Points : 4
Par défaut rep

dans ma table j'utilise l'hiritage
table produit id_produit
et les produits sont film(id_film)logiciel(id_logiciel)jeux(id_jeux)
marwen86 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/03/2010, 12h58   #6
Membre habitué
 
Inscription : juillet 2009
Messages : 156
Détails du profil
Informations forums :
Inscription : juillet 2009
Messages : 156
Points : 133
Points : 133
je n'avais pas vu tout ton code, donc tu récupère déjà ton id produit dans les lien
Code :
1
2
<td width="10%"><a href="action_supprimer_film.php?pg=<?php echo $ligne[id_produit]; ?>">Supprimer</a></td> 
<td width="10%"><a href="modifier_film.php?tyty=<?php echo $ligne[id_produit]; ?>">Modifier</a></td>
donc derrière sur ton formulaire:
Code :
<input name="id_produit" type="hidden" value="$_GET['tyty']" />
tu ajoute ca dans ton deuxième formulaire
et dans les action tu récupère ton $_POST['id_produit']

ensuite tu peux faire ton UPDATE avec la condition "WHERE id_produit="
puisque tu l'aura récupéré, par contre 2 choses,

évites des variables du genre a, b, c, d, e etc...
protèges tes rentrées sql avec au moins "mysql_real_escape_string"
http://php.net/manual/fr/function.my...ape-string.php

Pour éviter de te prendre des injection sql...
sans ca, on peut facilement modifier ta requête...
puis l'avantage du "sprintf" c'est que tu dis le type de tes entrées... donc ce qui est numérique le restera sinon ton script crash... et c'est pas plus mal
Ze AzAr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/03/2010, 11h47   #7
Invité de passage
 
marwen mlayah
Inscription : décembre 2009
Messages : 31
Détails du profil
Informations personnelles :
Nom : marwen mlayah

Informations forums :
Inscription : décembre 2009
Messages : 31
Points : 4
Points : 4
n'a pas marché
la probleme est au niveau :
Code :
1
2
<td>Identifient</td>
	<td><input name="id_produit" type="hidden" value="$_GET['ty']" /></td>
dans la page action,quand je veux utiliser id_produit il n'y a aucune action méme je tester avec une erreur dans mon code il n'est pas capable de trouver cette erreur

page modifier
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
 
  <table width="52%" align="center" border="1"background="image/fontpg.jpg">
    <tr>
	<td>Identifient</td>
	<td><input name="id_produit" type="hidden" value="$_GET['ty']" /></td>
	<tr>
      <td width="23%" height="40">titre:</td>
      <td width="77%"><input name="titre" type="text"  id="titre" width="332" /></td>
    </tr>
	<tr>
	<td width="23%" height="40">type:</td>
	<td><input name="type" type="text"  id="type" width="332" /></td>
	</tr>
    <tr>
      <td width="23%" height="40">icone:</td>
      <td><label>
        <input name="icone" type="file" id="icone" />
      </label></td>
    </tr>
    <tr>
      <td width="23%" height="40">posture:</td>
      <td><label>
        <input name="posture" type="file" id="posture" />
      </label></td>
    </tr>
    <tr>
      <td width="23%" height="40">systeme exploitation : </td>
      <td><input name="se" type="text"  id="sys" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">ann&eacute;e:</td>
      <td><input name="annee" type="text"  id="ann" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">reference:</td>
      <td><input name="ref" type="text"  id="ref" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">realisateur:</td>
      <td><input name="real" type="text"  id="rea" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">acteur:</td>
      <td><input name="acteur" type="text"  id="act" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">langue:</td>
      <td><input name="lang" type="text"  id="lan" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">origine:</td>
      <td><input name="origine" type="text"  id="or" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">dur&eacute;e:</td>
      <td><input name="duree" type="text"  id="dur" width="332" /></td>
    </tr>
    <tr>
      <td width="23%" height="40">prix:</td>
      <td><input name="prix" type="text"  id="pri" width="332" /></td>
    </tr>
	  <td width="23%" height="40">poids:</td>
	    <td><input name="poid" type="text"  id="poid" width="332" /></td>
  </table>
page action:
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
<?php
if(isset($_POST['id_produit']))
{
$host  = 'localhost';  // serveur 
	$user  = 'root';  // identifiant
	$mdp  = '';  // mot de passe
	$base  = 'ruspina';  // nom de la base
 
	// Connexion au serveur
	mysql_connect($host, $user, $mdp)
		or die('Impossible de se connecter au serveur '.$host);
 
	// Sélection d'une base
	mysql_select_db($base)
		or die('Impossible de sélectionner la base '.$base);
 
	$a= $_POST['titre'];
$z=$_FILES['icone']['name'];
$e=$_FILES['posture']['name'];
$r=$_POST['se'];
$t=$_POST['annee'];
$y=$_POST['ref'];
$u=$_POST['real'];
$o=$_POST['acteur'];
$p=$_POST['lang'];
$q=$_POST['origine'];
$s=$_POST['duree'];
$d=$_POST['prix'];
$f=$_POST['poid'];
$g=$_POST['type'];
$id=$_POST['id_produit'];
 
$req="update produit set id_produit='',type='$g',titre='$a',icone='$z',posture='$e', systeme_exploitation='$r',annee='$t',reference='$y',prix='$d',poids='$f' where id_produit=$id";
$res=mysql_query($req);
 
}
else
echo"nn";
?>
alors est ce qu'il y a une autre solution pour récupérer l'id_produit?
mercie
marwen86 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/03/2010, 13h00   #8
Membre habitué
 
Inscription : juillet 2009
Messages : 156
Détails du profil
Informations forums :
Inscription : juillet 2009
Messages : 156
Points : 133
Points : 133
quand tu affiche le code source de ta page modifier, tu as le input hidden dans les sources... tu as quoi sur cette ligne???
Ze AzAr est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/03/2010, 18h56   #9
Invité de passage
 
marwen mlayah
Inscription : décembre 2009
Messages : 31
Détails du profil
Informations personnelles :
Nom : marwen mlayah

Informations forums :
Inscription : décembre 2009
Messages : 31
Points : 4
Points : 4
rien mais tout simplement je suivre ton aide
ce vous que me dite ajouter ca mais pour moi j'ai pas compris hiden? et ou je veux le placer?
marwen86 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 +1. Il est actuellement 04h14.


 
 
 
 
Partenaires

Hébergement Web