Afficher image de la base de données php

Résolu/Fermé
miichel999 Messages postés 20 Date d'inscription mercredi 18 mai 2016 Statut Membre Dernière intervention 23 mai 2016 - Modifié par jordane45 le 19/05/2016 à 01:34
miichel999 Messages postés 20 Date d'inscription mercredi 18 mai 2016 Statut Membre Dernière intervention 23 mai 2016 - 23 mai 2016 à 02:58
Salut,
Comment afficher une image de base de donnée de type longblob,
j'ai essayé ce code mais l'image ne s'affiche pas
Merci d'avance si vous trouvez la solution!

la base est :
CREATE TABLE IF NOT EXISTS `produits` (
  `id` int(30) NOT NULL AUTO_INCREMENT,
  `Nom_de_produit` varchar(50) NOT NULL,
  `img` longblob NOT NULL,
  `description` text NOT NULL,
  `prix` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;

interface affichage est :
<?php
// Sous WAMP (Windows)
$bdd = new PDO('mysql:host=localhost;dbname=base_phoenixkids;charset=utf8', 'root', '');


$reponse = $bdd->query('SELECT * FROM produits');

// On affiche chaque entrée une à une
while ($produits = $reponse->fetch())
{
?>
    <table>
  <th><img width="200px" height="200px" src="<?php echo $produits['img'];?>"/></th> 
  <th style="color:#0404B4"><?php echo $produits['prix']; ?>  Dinars</th>
    <th ><span style="color:orange"><?php echo $produits['Nom_de_produit']; ?></span></br>
    
    <?php echo $produits['description']; ?></th>
 
   </table>
<?php
}

$reponse->closeCursor(); // Termine le traitement de la requête

?>


EDIT : Ajout des BALISES DE CODE (jordane)

2 réponses

miichel999 Messages postés 20 Date d'inscription mercredi 18 mai 2016 Statut Membre Dernière intervention 23 mai 2016 2
Modifié par jordane45 le 21/05/2016 à 01:12
le code corrige est :
interface_insertion.php
<html>
  <head>
    <title>insertion de données</title>
  </head>
<body>
<div>
<form name="insertion" action="interface_insertion.php" method="POST" enctype="multipart/form-data"  >
  <table border="0" align="center" cellspacing="2" cellpadding="2">
    <tr align="center">
      <td>Nom de produit</td>
      <td><input type="text" name="Nom_de_produit"></td>
    </tr>
    <tr align="center">
      <td>Prix</td>
      <td><input type="text" name="prix"></td>
    </tr>
    <tr align="center">
      <td>Description</td>
      <td><textarea type="text" name="description"></textarea></td>
    </tr>
    <tr align="center">
      <td>Inserer image</td>
      <td><input type="file" name="img"></td>
    </tr>
   
    <tr align="center">
      <td colspan="2"><input type="submit" value="insérer" name="inserer"></td>
    </tr>
 
  </table>
</form>
</div>


</body>
</html>

<?php


  


//connexion à la BDD
require_once "cnxBdd.php";
 
 if(isset($_POST['inserer'])) {
if (isset($_POST['Nom_de_produit']) AND isset($_POST['prix']) AND isset($_POST['description']) AND isset($_FILES['img']['tmp_name'] )) 

 extract($_POST);
 extract($_FILES);
 if (!empty($Nom_de_produit) && !empty($prix) && !empty($description) && !empty($img))
 
 
{
$req = $bdd->prepare("INSERT INTO produits( Nom_de_produit, prix, description,img ) VALUES(?,?,?,?)"); 
     
$req->execute(array(
 $_POST['Nom_de_produit'] ,
 $_POST['prix'],
 $_POST['description'],
 $_FILES['img']['tmp_name']
 
 ));
    echo 'Le produit a bien été ajouté !';
}
 else 
 {
  echo "Vous n'aver pas remplit tous les champs ";
  } 
 }
?>


<style>
input[type=text], select {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

input[type=submit] {
    width: 100%;
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

input[type=submit]:hover {
    background-color: #45a049;
}

div {
    border-radius: 5px;
    background-color: #f2f2f2;
    padding: 40px;
}
</style>



interface d'affichage
produit_educatifs.php :


<div id="tableau_produit">
<?php



//Affichage des erreurs php
error_reporting(E_ALL);

//connexion à la BDD
require_once "cnxBdd.php";

//Requete et affichage:
try{
$reponse = $bdd->query('SELECT * FROM produits');
}catch(Exception $e){ //en cas d'erreur dans la requete
echo "Erreur dans la requete !".$e->getMessage();
}
// On affiche chaque entrée une à une
while ($produits = $reponse->fetch()) {

echo "<table>
<tbody>
<tr>
<td><img widtd='200px' height='200px' src='data:image/jpeg;base64,".base64_encode($produits['img'])."'></img></td>
<td style='color:#0404B4'>".$produits['prix']." Dinars</td>
<td ><span style='color:orange'>".$produits['Nom_de_produit']."</span></br>".$produits['description']."</td>
</tr>
</tbody>
</table>";
}

$reponse->closeCursor(); // Termine le traitement de la requête

?>




</div>


meme probleme pas d'affichage d'image
et j'ai pas utiliser file_get_contents


EDIT : Ajout du LANGAGE dans les balises de code (la coloration syntaxique).
Explications disponibles ici : ICI

Merci d'y penser dans tes prochains messages.
2
jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 4 650
21 mai 2016 à 01:13

meme probleme pas d'affichage d'image
et j'ai pas utiliser file_get_contents

Ben.... si tu ne l'as pas utilisé ... alors qu'on te dit de le faire ....... ne viens pas nous voir pour nous dire que ça ne marche pas.......!!!!!
On t'a expliqué quoi mettre ... je t'ai donné un lien qui donne un exemple ..... tu n'as plus qu'à lire ce qu'on te marque et à l'appliquer !!
-2
miichel999 Messages postés 20 Date d'inscription mercredi 18 mai 2016 Statut Membre Dernière intervention 23 mai 2016 2
23 mai 2016 à 02:58
merci beaucoup de vos aides,
Le probléme est dans l'insertion,
et ce code l'affichage est juste

echo "<img src='data:image/jpeg;base64".base64_encode($produits['img'])."'>";
0