Bonjour,
J’ai une petite galerie photos dont les images sont stockées via mysql.
J’ai donc un table "photosnap" :
Code:
1
2
3
4
5
6
7
8
9 CREATE TABLE `photosnap` ( `id` mediumint(75) NOT NULL auto_increment, `photo` text NOT NULL, `prenom` varchar(25) NOT NULL default 'Snapi', `note` char(20) NOT NULL, `date` datetime default NULL, `idsnap` smallint(6) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 AUTO_INCREMENT=23 ;
Maintenant pour donnée la possibilité a mes internautes de passage de laissé un commentaire sur tel ou tel photo j'ai crée un autre table nomée photosnapcom:
Code:
1
2
3
4
5
6 CREATE TABLE `photosnapcom` ( `id` smallint(6) unsigned NOT NULL auto_increment, `nom` varchar(128) default NULL, `commentaire` text, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 AUTO_INCREMENT=23 ;
j'ai ensuite crée un page qui permet d'afficher les photos : photo-snap.php
Ensuite j'ai créer un formulaire pour l'ajout des commentaires en fonctione de la photo. photo-snap-com.phpCode:
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 <?php $nb = 3; // Nombre d'affichages souhaités sur une ligne // requête SQL $sql = "SELECT *, DATE_FORMAT(date,'%d/%m/%Y à %H:%i:%S') as date FROM `photosnap` ORDER BY `id` DESC"; // envoie de la requête $req = mysql_query($sql) or die('<u>Probleme SQL</u> : '.$sql.'<br>'.mysql_error ()); // affichage des résultats avec $nb résultats par ligne echo'<table>'; $i = 1; while($resultat = mysql_fetch_array($req)) { if($i == 1) { echo'<tr>'; } echo'<td align="center"> <div id="cadresnap"> <img src="../snap/' , $resultat['photo'] , '" alt="' , $resultat['prenom'] , ' a mis sa trombine sur photos vosges" /></br> ' , $resultat['prenom'] , '</br> ' , $resultat['date'] , '</br> <a href=\'photo-snap-com.php?id=' .$resultat['id'].'\'><img src="../images/com.png" alt="ajouter un commentaire" border="0"></a> </div> </td>'; $i++; if($i > $nb) { echo'</tr>'; $i = 1; } } echo'</table>'; ?>
puis pour finir, j'ai créé une page photo-snap-valide.phpCode:
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 <?php $cnx = mysql_connect(xxxx xxxx xxxx); $db = mysql_select_db( xxxx ) ; $id = $_GET["id"] ; $sql = "SELECT * FROM photosnap WHERE id = ".$id ; //exécution de la requête: $requete = mysql_query( $sql, $cnx ); //affichage des données: if( $result = mysql_fetch_object( $requete ) ) { ?> <?php echo($id) ;?></p> <form name="modification" action="photo-snap-valide.php" method="POST" enctype="multipart/form-data"> <input type="hidden" name="id" value="<?php echo($id) ;?>"> <table border="0" cellspacing="10" cellpadding="0"> <tr> <td><div align="right">Nom </div></td> <td><input type="text" name="nom" value=""/></td> </tr> <tr> <td><div align="right">Commentaire</div></td> <td><textarea name="commentaire" id="commentaire" maxlength="255" ></textarea></td> </tr> <tr> <td> </td> <td><input name="submit" type="submit" value="Poster votre commentaire" /></td> </tr> </table> <p> </p> </form> <?php }//fin if ?>
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 <?PHP $db = mysql_connect(xxxxx xxxxxx xxxxxx); mysql_select_db(xxxxxx,$db); $sql = "INSERT INTO photosnapcom(id, nom, commentaire) VALUES('$id','$nom','$commentaire')"; mysql_query($sql) or die('Erreur SQL !'.$sql.' '.mysql_error()); echo 'Votre commentaire a été ajouter.'; mysql_close(); ?>
voilà ! donc mon probleme est le suivant :
je ne peu mettre qu'un seul commentaire sur les photos car si on essai d'en ajouté un deuxieme j'ai ce message d'erreur qui s'affiche
est ce que quelqu'un pourrait m'aider a resoudre ce probleme ?Code:Erreur SQL !INSERT INTO photosnapcom(id, nom, commentaire) VALUES('22','fabrice','essai commentaire numero 2') Duplicate entry '22' for key 1