J'ai comme exercice de créer une base de donnée pays et de créer une table t_pays
dans cette table j'ai 4 champs id_pays, nom_pays, capital_pays, image
Je dois maintenant manipuler des enregistrements, c'est à dire ajouter / modifier / supprimer des éléments tout fonctionne bien !
Là où je galère c'est au niveau de l'ajout de l'image, elle ne s'affiche pas et elle n'est pas envoyé dans mon dossier images (sur wamp)
http://hpics.li/436a6b9
Code : Tout sélectionner
<?php
$Temoin = 0;
$pdo = new pdo('mysql:host=localhost;dbname=pays','root',
"");
header("Content-Type: text/html; charset=utf-8");
include ("resize.php");
$choix = "";
if(isset ($_POST['GoToAjouter']))
{
$choix="GoToAjouter";
}
if(isset ($_POST['Ajouter']))
{
$choix="Ajouter";
}
if(isset ($_POST['Supprimer']))
{
$Temoin=1;
}
if(isset ($_POST['Modifier']))
{
$choix="Modifier";
}
if(isset ($_POST['GoToModifier']))
{
$choix="GoToModifier";
}
switch ($choix)
{
default:
if ($Temoin){
if(isset ($_POST['Check'])){
$check=$_POST['Check'];
$sql="DELETE FROM `pays`.`t_pays` WHERE `t_pays`.`id_pays` = ".$check[0].";";
$pdo->exec($sql);
}
}
$sql="select*from t_pays";
$stmt=$pdo->query($sql);
$arr=$stmt->fetchall();
echo "<div align='center' style='background-color:#9AA0E3; border:1px solid #756020; border-width:4 ; margin: 200 500; padding: 25'>
<form name='Form' action='index.php' method='POST'>
<table style='background-color:white ; padding: 5;' border=1>";
for ($I = 0 ; $I<count($arr); $I++){
echo "<tr>" ;
$enr=$arr[$I];
// ici on ajoute à chaque fois un champ pour notre tableau
echo "<td>".$enr[0]."</td>
<td>".$enr[1]."</td>
<td>".$enr[2]."</td>
<td><img src ='".$enr[3]."'/img></td>
<td><input type='checkbox' name='Check[]' value='".$enr[0]."'/></td>";
}
echo "</table> </br>
<input type='submit' name='Supprimer' value='Supprimer'/>
<input type='submit' name='GoToAjouter' value='Ajouter' />
<input type='submit' name='GoToModifier' value='Modifier' />
</form></div></body>";
break;
case "GoToAjouter":
echo "<div align='center' style='background-color:#9AA0E3; border:1px solid #756020; border-width:4 ; margin: 200 500; padding: 25'>";
echo '<form name="Form" action="index.php" method="POST">
<tab><tr><td>
Nom Pays <input type="text" name="nom_pays" value=""/></br>
Capital <input type="text" name="capital_pays" value=""/></br>
<input type="file" name="image"/></td></tr></tab>
<input type="submit" name="Ajouter" value="Ajouter dans database"/>
<input type="submit" name="Lister" Value="Lister les enregistrements"/>
</form></div>';
break;
// pour ajouter des enrengistrements
case "Ajouter":
$nom_pays=$_POST['nom_pays'];
$capital_pays=$_POST['capital_pays'];
if (is_uploaded_file($_FILES['Image']['tmp_name']))
{$tmp_name = $_FILES['Image']['tmp_name'];
$newimage = $_FILES['Image']['name'];
move_uploaded_file($tmp_name,"images/".$newimage);
resize($newimage);
}
$sql="INSERT INTO `pays`.`t_pays` (`id_pays`, `nom_pays`, `capital_pays`, `image`) VALUES (NULL, '".$nom_pays."', '".$capital_pays."', '".$newimage."');";
echo $sql;
$pdo->exec($sql);
header("location:index.php");
break;Code : Tout sélectionner
<?php
function resize($fichier)
{
$dir="images/";
$fichierSource = $dir.$fichier;
$source = ImageCreateFromJpeg($fichierSource);
$largeurSource = imagesx($source);
$hauteurSource = imagesy($source);
$ratiol=100/$largeurSource;
$largeurDestination=100;
$hauteurDestination=$hauteurSource*$ratiol;
$im = ImageCreateTrueColor ($largeurDestination, $hauteurDestination)
or die ("Erreur lors de la création de l'image");
ImageCopyResampled($im, $source, 0, 0, 0, 0, $largeurDestination, $hauteurDestination, $largeurSource, $hauteurSource);
$miniature = $dir.$fichier;
ImageJpeg ($im, $miniature);
}
?>