Mettre a jour base de donnée php par formulaire
Résolu
miichel999
Messages postés
21
Statut
Membre
-
miichel999 Messages postés 21 Statut Membre -
miichel999 Messages postés 21 Statut Membre -
Salut,
je suis débutant en language php, je veut mettre a jour une base de donneés par un formulaire, je met le ce code mais ne marche pas .
Merci d'avance si vous trouvez la solution!
base de donnée:
CREATE TABLE IF NOT EXISTS `produits` (
`id` int(30) NOT NULL AUTO_INCREMENT,
`Nom_de_produit` varchar(50) NOT NULL,
`img` blob NOT NULL,
`description` text NOT NULL,
`prix` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
interface_modification.php :
<?php
$bdd = new PDO('mysql:host=localhost;dbname=base_phoenixkids;charset=utf8', 'root', '');
$reponse = $bdd->query('SELECT * FROM produits ORDER BY id ');
while ($produits = $reponse->fetch())
{
?>
<table>
<th><?php echo $produits['Nom_de_produit']; ?></th>
<th><?php echo $produits['prix']; ?></th>
<th><?php echo $produits['description']; ?></th>
<th><img width="200px" height="200px" src="<?php echo $produits['img'];?>"/></th>
<th><a href="modification2.php?id='<?php echo $produits['id'];?>'">Modifier</a></th>
</table>
<?php
}
?>
modification2.php :
<?php
$bdd = new PDO('mysql:host=localhost;dbname=base_phoenixkids;charset=utf8', 'root', '');
$req = $bdd->prepare('SELECT * FROM produits WHERE id='.$_GET['id'] );
$req->execute();
while ($produits = $req->fetch())
{
?>
<form name="modification" action="modification3.php" method="post">
<input type="hidden" name="id" value="<?php echo($_GET['id']) ;?>">
<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" value="<?php echo($produits['Nom_de_produit']) ;?>"></td>
</tr>
<tr align="center">
<td>prix</td>
<td><input type="text" name="prix" value="<?php echo($produits['prix']) ;?>"></td>
</tr>
<tr align="center">
<td>Description</td>
<td><input type="text" name="description" value="<?php echo($produits['description']) ;?>"></td>
</tr>
<tr align="center">
<td>Image</td>
<td><input type="file" name="img" value="<?php echo($produits['img']) ;?>"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="modifier" name="modifier"></td>
</tr>
</table>
</form>
<?php
}
?>
modification3.php:
<?php
$bdd = new PDO('mysql:host=localhost;dbname=base_phoenixkids;charset=utf8', 'root', '');
if(isset($_POST['modifier'])) {
if (isset($_POST['Nom_de_produit']) AND isset($_POST['prix']) AND isset($_POST['description']) AND isset($_POST['img'] ))
{
extract($_POST);
if (!empty($Nom_de_produit) && !empty($prix) && !empty($description) && !empty($img))
{
$req = $bdd->prepare('UPDATE produits
SET Nom_de_produit = :np ,
prix = :p,
description = :d,
img = :im
WHERE id = :i ');
$req->execute(array(
'np' => $Nom_de_produit,
'p' => $prix,
'd' => $description,
'im' => $img,
'i' => $id ));
echo 'Le produit a bien été modifier !';}
ELSE {
echo "veuiller entrer tous les information de produit";
}
}}
?>
je suis débutant en language php, je veut mettre a jour une base de donneés par un formulaire, je met le ce code mais ne marche pas .
Merci d'avance si vous trouvez la solution!
base de donnée:
CREATE TABLE IF NOT EXISTS `produits` (
`id` int(30) NOT NULL AUTO_INCREMENT,
`Nom_de_produit` varchar(50) NOT NULL,
`img` blob NOT NULL,
`description` text NOT NULL,
`prix` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
interface_modification.php :
<?php
$bdd = new PDO('mysql:host=localhost;dbname=base_phoenixkids;charset=utf8', 'root', '');
$reponse = $bdd->query('SELECT * FROM produits ORDER BY id ');
while ($produits = $reponse->fetch())
{
?>
<table>
<th><?php echo $produits['Nom_de_produit']; ?></th>
<th><?php echo $produits['prix']; ?></th>
<th><?php echo $produits['description']; ?></th>
<th><img width="200px" height="200px" src="<?php echo $produits['img'];?>"/></th>
<th><a href="modification2.php?id='<?php echo $produits['id'];?>'">Modifier</a></th>
</table>
<?php
}
?>
modification2.php :
<?php
$bdd = new PDO('mysql:host=localhost;dbname=base_phoenixkids;charset=utf8', 'root', '');
$req = $bdd->prepare('SELECT * FROM produits WHERE id='.$_GET['id'] );
$req->execute();
while ($produits = $req->fetch())
{
?>
<form name="modification" action="modification3.php" method="post">
<input type="hidden" name="id" value="<?php echo($_GET['id']) ;?>">
<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" value="<?php echo($produits['Nom_de_produit']) ;?>"></td>
</tr>
<tr align="center">
<td>prix</td>
<td><input type="text" name="prix" value="<?php echo($produits['prix']) ;?>"></td>
</tr>
<tr align="center">
<td>Description</td>
<td><input type="text" name="description" value="<?php echo($produits['description']) ;?>"></td>
</tr>
<tr align="center">
<td>Image</td>
<td><input type="file" name="img" value="<?php echo($produits['img']) ;?>"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="modifier" name="modifier"></td>
</tr>
</table>
</form>
<?php
}
?>
modification3.php:
<?php
$bdd = new PDO('mysql:host=localhost;dbname=base_phoenixkids;charset=utf8', 'root', '');
if(isset($_POST['modifier'])) {
if (isset($_POST['Nom_de_produit']) AND isset($_POST['prix']) AND isset($_POST['description']) AND isset($_POST['img'] ))
{
extract($_POST);
if (!empty($Nom_de_produit) && !empty($prix) && !empty($description) && !empty($img))
{
$req = $bdd->prepare('UPDATE produits
SET Nom_de_produit = :np ,
prix = :p,
description = :d,
img = :im
WHERE id = :i ');
$req->execute(array(
'np' => $Nom_de_produit,
'p' => $prix,
'd' => $description,
'im' => $img,
'i' => $id ));
echo 'Le produit a bien été modifier !';}
ELSE {
echo "veuiller entrer tous les information de produit";
}
}}
?>
2 réponses
-
Autant pour moi, j'avais oublier à quel point PDO était logique.
Le truc c'est que tu fais un echo aprés ta requéte, mais tu ne sais pas si elle c'est executer.
Essaye :
$req = $bdd->prepare('UPDATE produits
SET Nom_de_produit = :np,
prix = :p,
description = :d,
img = :im
WHERE id = :i ');
$req->bindParam(":np", $Nom_de_produit);
$req->bindParam(":p", $prix);
$req->bindParam(":d", $description);
$req->bindParam(":im", $img);
$req->bindParam(":i", $id);
if($req->execute())
{
echo 'Le produit a bien été modifier !';
}
else
{
echo 'une erreur est survenue';
}
N'oubliez pas de marquez votre post comme résolut lorsque vous avez eu les réponses à vos questions...
Au royaume des aveugles, les borgnes sont rois.-
-
Esseyons autre chose alors. Avec gestion des erreurs pour en savoir plus.
Remplace tout ce qu'il y a dans ton
if (!empty($Nom_de_produit) && !empty($prix) && !empty($description) && !empty($img))
par :
$sql = "UPDATE produits SET Nom_de_produit = :np, prix = :p, description = :d, img = :im WHERE id = :i";
$req = $pdo->prepare($sql);
$req->bindParam(":np", $Nom_de_produit);
$req->bindParam(":p", $prix);
$req->bindParam(":d", $description);
$req->bindParam(":im", $img);
$req->bindParam(":i", $id);
try
{
$result = $req->execute();
}
catch(PDOException $e)
{
echo $e->getCode() . " - " . $e->getMessage();
} -
-
-
-
-
Salut,
Sa serait bien d'indiquer le probléme que tu rencontres non ?
Déja, aprés avoir survoler le code (pas trés organisé), tu peux déjà modifier :
while ($produits = $reponse->fetch())
par :
while ($produits == $reponse->fetch())