IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

update d'un table de bd


Sujet :

PHP & Base de données

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Décembre 2009
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Décembre 2009
    Messages : 31
    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 : 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
    <?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 : Sélectionner tout - Visualiser dans une fenêtre à part
    <a href="modifier_film.php?tyty=<?php echo $ligne[id_produit]; ?>">Modifier</a>
    lien vers cette page:

    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
    <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 : 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
    <?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

  2. #2
    Membre éprouvé
    Inscrit en
    Juillet 2009
    Messages
    156
    Détails du profil
    Informations forums :
    Inscription : Juillet 2009
    Messages : 156
    Par défaut
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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...

  3. #3
    Membre averti
    Inscrit en
    Décembre 2009
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Décembre 2009
    Messages : 31
    Par défaut
    input hidden ??
    svp je suis débutant essayer de plus detailler
    svp
    merci

  4. #4
    Membre éprouvé
    Inscrit en
    Juillet 2009
    Messages
    156
    Détails du profil
    Informations forums :
    Inscription : Juillet 2009
    Messages : 156
    Par défaut
    quel est le id unique de ta table???

  5. #5
    Membre averti
    Inscrit en
    Décembre 2009
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Décembre 2009
    Messages : 31
    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)

  6. #6
    Membre éprouvé
    Inscrit en
    Juillet 2009
    Messages
    156
    Détails du profil
    Informations forums :
    Inscription : Juillet 2009
    Messages : 156
    Par défaut
    je n'avais pas vu tout ton code, donc tu récupère déjà ton id produit dans les lien
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    <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

Discussions similaires

  1. Update d'une table a partir d'une autre table
    Par Yannis06 dans le forum Oracle
    Réponses: 6
    Dernier message: 11/08/2005, 11h32
  2. Update d'une table
    Par EssaiEncore dans le forum Langage SQL
    Réponses: 2
    Dernier message: 18/02/2005, 10h00
  3. Tquery - update avec 2 tables
    Par spocksb dans le forum Bases de données
    Réponses: 6
    Dernier message: 16/08/2004, 08h39
  4. UPDATE d'une table avec test d'un champ d'une autre table
    Par delphim dans le forum Langage SQL
    Réponses: 2
    Dernier message: 03/05/2004, 12h30
  5. Update entre 2 tables
    Par jfox dans le forum SQL
    Réponses: 8
    Dernier message: 04/11/2003, 10h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo