Mettre a jour base de donnée php par formulaire
Résolu
miichel999
Messages postés
20
Date d'inscription
Statut
Membre
Dernière intervention
-
miichel999 Messages postés 20 Date d'inscription Statut Membre Dernière intervention -
miichel999 Messages postés 20 Date d'inscription Statut Membre Dernière intervention -
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";
}
}}
?>
A voir également:
- Mettre a jour base de donnée php par formulaire
- Whatsapp formulaire opposition - Guide
- Mettre a jour chrome - Accueil - Applications & Logiciels
- Formulaire de réclamation facebook - Guide
- Mise a jour windows 10 - Accueil - Mise à jour
- Mettre a jour chromecast - Accueil - Guide TV et vidéo
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.
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.
reste le méme probleme
ça m'affiche 'Le produit a bien été modifier !'
mais la base de donnee reste la méme
Remplace tout ce qu'il y a dans ton
par :
( ! ) Notice: Undefined variable: pdo in C:\wamp\www\phoenixkids1\modification3.php on line 11
( ! ) Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\phoenixkids1\modification3.php on line 11
la ligne 11 est : $req = $pdo->prepare($sql);
Remplace $pdo par $bdd vu que c'est dans cette variable que tu as défini la connexion.
pas de message d'erreur
et pas de changement dans la base