Ajouter des images

cs_samia22 Messages postés 16 Date d'inscription lundi 28 mars 2011 Statut Membre Dernière intervention 28 mai 2011 - 27 avril 2011 à 21:14
cs_samia22 Messages postés 16 Date d'inscription lundi 28 mars 2011 Statut Membre Dernière intervention 28 mai 2011 - 28 avril 2011 à 12:26
Bonsoir;

j'ai un probleme...
je travaille coté administrateur...
j'ajout des produits a la base de données avec ses images...

l'ajout de produits marche bien...
mais le probleme est l'image de chaque produit ne s'affiche pas...
*je suis tres nerveux...

aidez moi ..

php:5.3.3.5
la table produit :idproduit int(12) auto_increment
reference varchar(20)
nomprodui tvarchar(20)
description varchar(20)
prix double
idcategorie int(12) clé etrangere de table catégorie
marque varchar(20)
promotion varchar(20)
TVA double
img blob

Voici le code d'ajout:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Ajouter un produit</title>
<meta http-equiv= "Content-Type" content="text/html; charset=iso-8859-1">
</head>


<?php
include('fonctions.php');
session_start();
if(isset($_SESSION['login'])){


?>
<center>
Ajouter un nouveau produit

<form action="ajouter.php" method="POST">
Catégorie |<select name ="titre">
<?php

connexion();
$query = 'SELECT * FROM `categorie` WHERE `idcategorie`<>`titre`;';
$result = mysql_query($query) or die("erreur!!");
$i = 0;
while($row = mysql_fetch_row($result)){
$i++;
echo "<option value=\"".$row[0]."\">".$row[1];
}
if($i == 0){
?>
<script type="text/javascript">
alert("aucune categorie disponible!!");
window.location.replace("GestionProduits.php");
</script>
<?php
}
mysql_close();
?>
</select>,

----
Réference: |,
----
Nom: |,
----
Description |:</th>,
----
Prix: |,
----
Marque: |,
----
Promotion: |,
----
Quantité Stock: |,
----
TVA: |,
----
image: |




</form>
</center>
<?php

if(isset($_POST['titre']) and isset($_POST['reference']) and isset($_POST['nomproduit']) and isset($_POST['description']) and isset($_POST['prix'])
and isset($_POST['marque']) and isset($_POST['promotion']) and isset($_POST['QteStock']) and isset($_POST['TVA']) and isset($_POST['img']))
{
if(!empty($_POST['titre']) and !empty($_POST['reference']) and !empty($_POST['nomproduit']) and !empty($_POST['description']) and !empty($_POST['prix'])
and !empty($_POST['marque']) and !empty($_POST['promotion']) and !empty($_POST['QteStock']) and !empty($_POST['TVA']) and !empty($_POST['img']))
{
connexion();
$sql1 ="select * from produit where reference='".$_POST['reference']."'";
$resultat=mysql_query($sql1) or die('erreur');
if(mysql_num_rows($resultat)==0)
{
$sql2="insert into produit(reference,nomproduit,description,prix,idcategorie,marque,promotion,QteStock,TVA,img) values('".$_POST['reference']."','".$_POST['nomproduit']."','".$_POST['description']."','".$_POST['prix']."',
'".$_POST['titre']."','".$_POST['marque']."','".$_POST['promotion']."','".$_POST['QteStock']."','".$_POST['TVA']."','".$_POST['img']."')";
mysql_query($sql2);
deconnexion();
alerte("Le produit ".$_POST['nomproduit']." est ajouté avec succés");
}
else
alerte('le produit existant');
}
else
alerte('Remplir les champs');
}

?>

</html>
<?php
}else{
?>
<script language="javascript" type="text/javascript">
<!--
window.location.replace("index.php");
-->
</script>
<?php
}
?>

merci bien

8 réponses

cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
27 avril 2011 à 22:13
BONSOIR
dans <form ...
il anque déjà
ENCTYPE = "multipart/form-data"
a++

Bonne programmation !
0
cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
27 avril 2011 à 22:21
et faudrait un truc comme ça pour enregistrer

$img = "image.jpg";
$fp = fopen($img,"r");
$data = fread($fp,filesize($img));
fclose($fp);
$data = addslashes($data);

puis insert $data


mais le blob pour stocker des images c'est pas conseiller mieux
stocker un lien vers le repertoire qui contient l'image ...

Bonne programmation !
0
cs_samia22 Messages postés 16 Date d'inscription lundi 28 mars 2011 Statut Membre Dernière intervention 28 mai 2011
28 avril 2011 à 06:24
bonjour;
desolé ca marche pas
voici le code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Ajouter un produit</title>
<meta http-equiv= "Content-Type" content="text/html; charset=iso-8859-1">
</head>


<?php
include('fonctions.php');
session_start();
if(isset($_SESSION['login'])){


?>
<center>
Ajouter un nouveau produit

<form action="ajouter.php" method="POST" ENCTYPE = "multipart/form-data" >
Catégorie |<select name ="titre">
<?php

connexion();
$query = 'SELECT * FROM `categorie` WHERE `idcategorie`<>`titre`;';
$result = mysql_query($query) or die("erreur!!");
$i = 0;
while($row = mysql_fetch_row($result)){
$i++;
echo "<option value=\"".$row[0]."\">".$row[1];
}
if($i == 0){
?>
<script type="text/javascript">
alert("aucune categorie disponible!!");
window.location.replace("GestionProduits.php");
</script>
<?php
}
mysql_close();
?>
</select>,

----
Réference: |,
----
Nom: |,
----
Description |:</th>,
----
Prix: |,
----
Marque: |,
----
Promotion: |,
----
Quantité Stock: |,
----
TVA: |,
----
image: |




</form>
</center>
<?php

if(isset($_POST['titre']) and isset($_POST['reference']) and isset($_POST['nomproduit']) and isset($_POST['description']) and isset($_POST['prix'])
and isset($_POST['marque']) and isset($_POST['promotion']) and isset($_POST['QteStock']) and isset($_POST['TVA']) and isset($_POST['img']))
{
if(!empty($_POST['titre']) and !empty($_POST['reference']) and !empty($_POST['nomproduit']) and !empty($_POST['description']) and !empty($_POST['prix'])
and !empty($_POST['marque']) and !empty($_POST['promotion']) and !empty($_POST['QteStock']) and !empty($_POST['TVA']) and !empty($_POST['img']))
{
connexion();
$img = "image.jpg";
$fp = fopen($img,"r");
$data = fread($fp,filesize($img));
fclose($fp);
$data = addslashes($data);



$sql1="select * from produit where reference='".$_POST['reference']."'";
$resultat=mysql_query($sql1) or die('erreur');
if(mysql_num_rows($resultat)==0)
{
$sql2="insert into produit(reference,nomproduit,description,prix,idcategorie,marque,promotion,QteStock,TVA,img) values('".$_POST['reference']."','".$_POST['nomproduit']."','".$_POST['description']."','".$_POST['prix']."',
'".$_POST['titre']."','".$_POST['marque']."','".$_POST['promotion']."','".$_POST['QteStock']."','".$_POST['TVA']."','".$data."')";
mysql_query($sql2);
deconnexion();
alerte("Le produit ".$_POST['nomproduit']." est ajouté avec succés");
}
else
alerte('le produit existant');
}
else
alerte('Remplir les champs');
}

?>

</html>
<?php
}else{
?>
<script language="javascript" type="text/javascript">
<!--
window.location.replace("index.php");
-->
</script>
<?php
}
?>
0
cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
28 avril 2011 à 11:24
bonjour

je traite que l'insertion
il faudra faire un header() ...
pour l'affichage du blob
neamoins tu peux verifier que ça marche
...

pour le reste cherche afficher un champs blob
sinon reposte ...

a++

<?php
//j'ai une table bidon qui reprend tes champs

/*
CREATE TABLE IF NOT EXISTS `produit` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `reference` varchar(50) NOT NULL,
  `nomproduit` text NOT NULL,
  `description` varchar(50) NOT NULL,
  `prix` float NOT NULL,
  `idcategorie` varchar(50) NOT NULL,
  `marque` varchar(50) NOT NULL,
  `promotion` varchar(5) NOT NULL,
  `QteStock` int(11) NOT NULL,
  `Tva` float NOT NULL,
  `img` longblob NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE= MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

*/  


mysql_connect('localhost','root','');
mysql_select_db('blob'); /*ici la tienne*/

if(isset($_GET['voir']) && !empty($_GET['voir'])){
 
 $last=(int)($_GET['voir']);
 $s=mysql_query("select img from produit where id='".$last."'");
    $r=mysql_fetch_array($s);
       
    if($r['img']!=""){
    
    $f=list($name,$type,$size,$content)=$r['img'];
    print_r($f);
    }else{
    //echo "rien";
    }
    
}else{
}

$msg="";

if (isset($_FILES['img']) && !empty($_FILES['img']['tmp_name']) ){

    $data = file_get_contents($_FILES['img']['tmp_name']);
    $data = mysql_real_escape_string($data);
    $msg = 'image chargée';
  
}else{
$data='';
$msg='pas d\'image chargée';
}

echo '<center>'.$msg.'</center>
';


/*le formulaire est envoyé*/

if(isset($_POST['sub'])){

  $_POST['titre']="un titre";
  
  $sql2="insert into produit(reference,nomproduit,description,prix,idcategorie,marque,promotion,QteStock,TVA,img) values('".$_POST['reference']."','".$_POST['nomproduit']."','".$_POST['description']."','".$_POST['prix']."', 
  '".$_POST['titre']."','".$_POST['marque']."','".$_POST['promotion']."','".$_POST['QteStock']."','".$_POST['TVA']."','".$data."')"; 
    if(mysql_query($sql2)){
    echo '<center>LIGNE INSERÉE  ['.$_SERVER['PHP_SELF'].' RETOUR AU FORMULAIRE]</center>';
    unset($_POST);  /*verif image blob*/
    
    $last=mysql_insert_id();
    
    echo '<center><hr>[?voir='.$last.' Verifier  + Voir l\'image]</center>
';
      
    }else{
    exit('ouuu !');
    }




/*sinon affichage du form*/

}else{?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>Ajouter un produit</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

 
<center> 
Ajouter un nouveau produit

<form action="" method="POST" ENCTYPE="multipart/form-data" />
 Catégorie  |<select name ="titre">
</select>,

----
Réference: |,
----
Nom: |,
----
Description |</th>,
----
Prix: |,
----
Marque: |,
----
Promotion: |,
----
Quantité Stock: |,
----
TVA: |,
----
image: |
 

 
    
</form> 
</center> 
 
</html> 
<?php
}
?>



Bonne programmation !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_samia22 Messages postés 16 Date d'inscription lundi 28 mars 2011 Statut Membre Dernière intervention 28 mai 2011
28 avril 2011 à 11:42
Bonjour;

je teste votre code ...
le produit s'ajoute dans la base de donnée...
mais il s'affiche
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\boutiqueWMC\administrateur\ajouter.php on line 9
( $r=mysql_fetch_array($s);)


et lorsque j'affiche l'image...il n'affiche pas l'image ..il m'affiche des caracteres n'importe quoi...
0
cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
28 avril 2011 à 11:59
en effet

les caracteres n'importe quoi c'est l'image
il faut avoir une fonction header() qui transforme les caractères en image
donc si tu verifies dans ta base via (ex phpmyadmin) tu as un champs blob
avec l'image au format brute
il faut remettre au format image pour afficher

/*attention ici jpg*/
header("Content-type: image/jpg");
print $row['image'];

si tu utilises mon code tel quel il marche tu as du mal adapter
mais il est que educatif ! donc

en realité si c'est pour un catalogue produit tu devrais pas utiliser
blob mais mettre le lien ou le nom du fichier uploadé par 'file' dans la base en varchar et uploader l'image dans un repertoire
blob va remplir ta base tres rapidement et les hebergeurs bloque les bases trop importantes, 20 images et ta base est saturée ...


Bonne programmation !
0
cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
28 avril 2011 à 12:08
http://www.phpriot.com/articles/images-in-mysql
un lien et un cours assez long

Bonne programmation !
0
cs_samia22 Messages postés 16 Date d'inscription lundi 28 mars 2011 Statut Membre Dernière intervention 28 mai 2011
28 avril 2011 à 12:26
merci cod54;
pour votre intention...

et merci pour votre aide


bonne journée
0
Rejoignez-nous