Suppression d'une ligne dans table MySQL
Résolu
leoetevan
Messages postés
110
Date d'inscription
Statut
Membre
Dernière intervention
-
leoetevan Messages postés 110 Date d'inscription Statut Membre Dernière intervention -
leoetevan Messages postés 110 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
Je suis en train de créer un site internet. Dans une page, je liste les valeurs (dans un tableau) que je récupère de ma base MySQL. Je voudrais placer un bouton "Supprimer" en bout de tableau pour pouvoir supprimer l'enregistrement situé sur cette même ligne.
J'ai bien placé mon bouton "Supprimer", j'arrive bien à supprimer un enregistrement sauf que se n'est jamais le bon! lol Je voudrais savoir comment je peux lier mon bouton à l'identifiant de la ligne correspondant.
Merci d'avance
Je suis en train de créer un site internet. Dans une page, je liste les valeurs (dans un tableau) que je récupère de ma base MySQL. Je voudrais placer un bouton "Supprimer" en bout de tableau pour pouvoir supprimer l'enregistrement situé sur cette même ligne.
J'ai bien placé mon bouton "Supprimer", j'arrive bien à supprimer un enregistrement sauf que se n'est jamais le bon! lol Je voudrais savoir comment je peux lier mon bouton à l'identifiant de la ligne correspondant.
Merci d'avance
A voir également:
- Suppression d'une ligne dans table MySQL
- Table ascii - Guide
- Forcer suppression fichier - Guide
- Table des matières word - Guide
- Partager photos en ligne - Guide
- Mètre en ligne - Guide
14 réponses
Avant d'afficher ta ligne tu connais l'id à laquelle elle correspond...
si tu utilises le bouton dans un formulaire <form> places un champ <input type="hidden" name="id" value="la_bonne_id" /> dans le formulaire, si tu utilises un lien du genre supprimer.php, transforme le en supprimer.php?id=la_bonne_id
et dans ta page de suppression récupères ton id dans la requete 'DELETE FROM xxx WHERE id = '.$_GET['la_bonne_id']
si tu utilises le bouton dans un formulaire <form> places un champ <input type="hidden" name="id" value="la_bonne_id" /> dans le formulaire, si tu utilises un lien du genre supprimer.php, transforme le en supprimer.php?id=la_bonne_id
et dans ta page de suppression récupères ton id dans la requete 'DELETE FROM xxx WHERE id = '.$_GET['la_bonne_id']
Voilà mon code:
<?php
if ((isset($_GET['idProjet'])) && ($_GET['idProjet'] != "")) {
$deleteSQL = sprintf("DELETE FROM projet WHERE idProj=%s",
GetSQLValueString($_GET['idProjet'], "int"));
mysql_select_db($database_ConnexionPortail, $ConnexionPortail);
$Result1 = mysql_query($deleteSQL, $ConnexionPortail) or die(mysql_error());
}
//Ma requette qui me permet de lister des projets
mysql_select_db($database, $Connexion);
$query_listProj = "SELECT projet.libelleProj, date_format(projet.dteDebProj,'%d/%m/%Y') as dteDebProjet, date_format(projet.dteFinProj,'%d/%m/%Y') as dteFinProjet, projet.idProj FROM projet WHERE projet.idEnt ='$userId' ORDER BY projet.dteDebProj";
$query_limit_listProj = sprintf("%s LIMIT %d, %d", $query_listProj, $startRow_listProj, $maxRows_listProj);
$listProj = mysql_query($query_limit_listProj, $ConnexionPortail) or die(mysql_error());
$row_listProj = mysql_fetch_assoc($listProj);
</code>
?>
<body>
<div>
<table>
<tr>
<th>Libellé du projet</th>
<th>Date de publication</th>
<th>Date de fin</th>
<th>Consulter</th>
<th>Supprimer</th>
</tr>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="submit" value="Supprimer" /></td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
</form>
</table>
</div>
<body>
Je ne sais pas comment lier mon bouton à "idProj".
En fait, il me supprime bien un enregistrement mais toujours le dernier qui à été enregistré dans la base et non pas celui qui se trouve sur la ligne du bouton cliqué
<?php
if ((isset($_GET['idProjet'])) && ($_GET['idProjet'] != "")) {
$deleteSQL = sprintf("DELETE FROM projet WHERE idProj=%s",
GetSQLValueString($_GET['idProjet'], "int"));
mysql_select_db($database_ConnexionPortail, $ConnexionPortail);
$Result1 = mysql_query($deleteSQL, $ConnexionPortail) or die(mysql_error());
}
//Ma requette qui me permet de lister des projets
mysql_select_db($database, $Connexion);
$query_listProj = "SELECT projet.libelleProj, date_format(projet.dteDebProj,'%d/%m/%Y') as dteDebProjet, date_format(projet.dteFinProj,'%d/%m/%Y') as dteFinProjet, projet.idProj FROM projet WHERE projet.idEnt ='$userId' ORDER BY projet.dteDebProj";
$query_limit_listProj = sprintf("%s LIMIT %d, %d", $query_listProj, $startRow_listProj, $maxRows_listProj);
$listProj = mysql_query($query_limit_listProj, $ConnexionPortail) or die(mysql_error());
$row_listProj = mysql_fetch_assoc($listProj);
</code>
?>
<body>
<div>
<table>
<tr>
<th>Libellé du projet</th>
<th>Date de publication</th>
<th>Date de fin</th>
<th>Consulter</th>
<th>Supprimer</th>
</tr>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="submit" value="Supprimer" /></td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
</form>
</table>
</div>
<body>
Je ne sais pas comment lier mon bouton à "idProj".
En fait, il me supprime bien un enregistrement mais toujours le dernier qui à été enregistré dans la base et non pas celui qui se trouve sur la ligne du bouton cliqué
Essaie ça :
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="hidden" name="idProjet" value="<?php echo $row_listProj['idProj']; ?>" /><input type="submit" value="Supprimer" /></td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
ça devrait passer
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="hidden" name="idProjet" value="<?php echo $row_listProj['idProj']; ?>" /><input type="submit" value="Supprimer" /></td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
ça devrait passer
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
et si tu faisais $deleteSQL = "DELETE FROM projet WHERE idProj=".$_GET[idProjet'];
au lieu de $deleteSQL = sprintf("DELETE FROM projet WHERE idProj=%s",
GetSQLValueString($_GET['idProjet'], "int"));
en conservant le code que je t'ai donné au dessus,
essaie de faire un petit echo $deleteSQL pour voir la requête telle qu'elle sera executée
au lieu de $deleteSQL = sprintf("DELETE FROM projet WHERE idProj=%s",
GetSQLValueString($_GET['idProjet'], "int"));
en conservant le code que je t'ai donné au dessus,
essaie de faire un petit echo $deleteSQL pour voir la requête telle qu'elle sera executée
J'ai bien fait se que tu m'as dit mais ça n'a rien changé (se qui veut aussi dire que le code précédent était tout aussi bon). L'echo du delete s'affiche bien avec le dernier id qui se trouve dans la table donc normal qu'il efface cette ligne.
N'y a t'il pas une instruction qui permettrait de lier un bouton à une variable précisément (celle qui se trouve sur la même ligne)?
Merci en tout cas de m'épaulé!! lol Je suis encore débutant.
N'y a t'il pas une instruction qui permettrait de lier un bouton à une variable précisément (celle qui se trouve sur la même ligne)?
Merci en tout cas de m'épaulé!! lol Je suis encore débutant.
euh... j'ai oublié une ' dans
$deleteSQL = "DELETE FROM projet WHERE idProj=".$_GET['idProjet'];
mais je ne pense pas que ça bloque à ce niveau... tu auras rectifié de toi même ;-)
Regarde le code généré, le value du champ caché doit être le même que celui de la colonne qui contient l'id
$deleteSQL = "DELETE FROM projet WHERE idProj=".$_GET['idProjet'];
mais je ne pense pas que ça bloque à ce niveau... tu auras rectifié de toi même ;-)
Regarde le code généré, le value du champ caché doit être le même que celui de la colonne qui contient l'id
Oui effectivement, j'avais corrigé la quote. lol
Pas de soucis au niveau du champ caché. C'est bien le même.
Pas de soucis au niveau du champ caché. C'est bien le même.
OK, j'ai trouvé (enfin je crois)
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="submit" value="Supprimer" /></td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
</form>
devient
<?php do { ?>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="submit" value="Supprimer" /></td>
</tr>
</form>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="submit" value="Supprimer" /></td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
</form>
devient
<?php do { ?>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="submit" value="Supprimer" /></td>
</tr>
</form>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<input type="hidden" name="idProjet" value="<?php echo $row_listProj['idProj']; ?>" />
<input type="submit" value="Supprimer" />
</form>
</td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<input type="hidden" name="idProjet" value="<?php echo $row_listProj['idProj']; ?>" />
<input type="submit" value="Supprimer" />
</form>
</td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
Non désolé, je retire se que je viens de dire.
je m'étais trompé j'avais mis ça:
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="submit" value="Supprimer" /></td>
</tr>
</form>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
Sous cette forme, le bouton de la première ligne fonctionne.
et quand j'ai corrigé en mettant la balise form sous celle du php, plus un seul bouton ne fonctionne. :(
je m'étais trompé j'avais mis ça:
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td><input type="submit" value="Supprimer" /></td>
</tr>
</form>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
Sous cette forme, le bouton de la première ligne fonctionne.
et quand j'ai corrigé en mettant la balise form sous celle du php, plus un seul bouton ne fonctionne. :(
Oui, là ton début de form était en dehors de la boucle
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<input type="hidden" name="idProjet" value="<?php echo $row_listProj['idProj']; ?>" />
<input type="submit" value="Supprimer" />
</form>
</td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
<?php do { ?>
<tr>
<td><?php echo $row_listProj['libelleProj']; ?></td>
<td><?php echo $row_listProj['dteDebProjet']; ?></td>
<td><?php echo $row_listProj['dteFinProjet']; ?></td>
<td><?php echo $row_listProj['idProj']; ?></td>
<td>
<form id="listeProj" name="listeProj" action="testMesProjets.php">
<input type="hidden" name="idProjet" value="<?php echo $row_listProj['idProj']; ?>" />
<input type="submit" value="Supprimer" />
</form>
</td>
</tr>
<?php } while ($row_listProj = mysql_fetch_assoc($listProj)); ?>
MERCIIIIIIIIIII
Cette fois, je crois que tu me sauve vraiment!!! Je commençais à me tirer les cheveux! Ça fonctionne impeccable.
Merci vraiment à ce forum et aux gens comme toi.
Cette fois, je crois que tu me sauve vraiment!!! Je commençais à me tirer les cheveux! Ça fonctionne impeccable.
Merci vraiment à ce forum et aux gens comme toi.