Bonjour,

Voila je rencontre un petit problème avec mon code.

je suis entrain de créer un petit tableau en html qui affiche mes donnée dans une tabl sql dans ma base de donner , ce que je veux faire c'est de metrre un button a la fin de chaque ligne (une ligne tt les information d'un client ) qui supprime ce client de la base de donné

<?php

$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('res_bd');

/* location 0 */

$query2 = " select id_bien,superficie,nbr_piece,adresse,departement,description_bien,prix,nom,prenom from bien,profil where (transaction = 0) and (id_agent = id_profil)"  ;

$locations = mysql_query($query2);

/* vente 1 */

$query3 = "select id_bien,superficie,nbr_piece,adresse,departement,description_bien,prix,nom,prenom from bien,profil where (transaction = 1) and (id_agent = id_profil) "  ;

$ventes = mysql_query($query3);

?>

<!DOCTYPE  html>
<!DOCTYPE html>
<html>
<head>
    <title> acceuil </title>
</head>
<body>

<br> <br> <br>

<form action="agent.php" method="POST" >

<?php

                echo "LISTE DES BIENS POUR LOCATION"; 

                echo "<table border = '1'>"; 
                echo 
                "<tr>
                <th>  Superficie maison  </th>
                <th>   Adresse </th>
                <th>   Departement </th>
                <th>   Nombre pieces </th>
                <th>   Description bien </th>
                <th>   Prix </th>

                <tr>";

    while($row = mysql_fetch_array($locations)){  

            echo "<tr>
            <td '>" . $row['superficie'] . "</td>
            <td>"  .$row['adresse']."</td>
            <td '>" . $row['departement'] . "</td>
            <td>"  . $row['nbr_piece'] . "</td>
            <td '>" . $row['description_bien'] . "</td>
            <td>"  . $row['prix'] . "</td>
            </tr>";
    }

?>

<br> <br> <br>

 <?php

                echo "LISTE DES BIEN PROPOSER A LA VENTE"; 

            echo "<table border = '1'>"; 
            echo 
            "<tr>
            <th>  Superficie maison  </th>
            <th>   Adresse </th>
            <th>   Departement </th>
            <th>   Nombre pieces </th>
            <th>   Description bien </th>
            <th>   Prix </th>
            <th>   </th>";

    while($row = mysql_fetch_array($ventes))
    {  

                echo "<tr>
                <td '>" . $row['superficie'] . "</td>
                <td>"  .$row['adresse']."</td>
                <td '>" . $row['departement'] . "</td>
                <td>"  . $row['nbr_piece'] . "</td>
                <td '>" . $row['description_bien'] . "</td>
                <td>"  . $row['prix'] . "</td>";
            }
                 ?>

 <!--<td> <input type="submit" name="vendu" value="Vendu"   id=". $row['id_bien'] .">  </td>-->

 <td> 
      <input type="submit" name="supprimer" value="Supprimer" >
        <input  type="hidden" name="b_id" value="<?=$fetcher['id_bien'] ?>"/>  

 </td>

<?php
echo " </tr> ";
?>
<?php

    if (isset($_POST['supprimer']))
            {
             $boking_id = (int) $_POST['b_id'];
             mysqli_query($connection, "DELETE FROM `bien` where id_bien= '".$boking_id."'");
            if(mysqli_affected_rows($connection))
            {  
                echo "success";
            }
            else{

              echo "faild";

               }
            }

if($_GET){
            if(isset($_POST[‘vendu’]))
            {
                 $id = $_GET['id_bien'];
                 $query4 = "UPDATE `bien` SET `disponible`= 0 where id_bien = $id ";
                 mysql_query($query4);
            }
            if (isset($_POST['supprimer']))
            {
             $boking_id =(int) $_POST['b_id'];
             mysqli_query($connection, "DELETE FROM `bien` where id_bien= '".$boking_id."'");
            if(mysqli_affected_rows($connection))
            {  
                echo "success";
            }
            else{

              echo "faild";

               }
            }
}

 ?>

</form>

</body>
</html>

2 réponses


Houlà ! Nous sommes en 2018 et PHP c'est la version 7.0 minimum qu'il FAUT utiliser.
mysql_* est déprécié depuis un moment déjà et il serait bien de se mettre à utiliser PDO.

Sinon, ta demande n'est pas très claire. Quel est le problème rencontré ? Quel est le message d'erreur qui est affiché (dans les logs ou sur ta page) ?

Voici un code beaucoup plus propre :

<?php

// Je sais que tu va juste copier coller
// mais vraiment apprend le dev et avec des
// tutoriels qui sont à jour.

// Connexion à la base de données
// + configuration
try {
    $db = new PDO('mysql:host=localhost;dbname=res_bd', 'root', '');
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $exception) {
    echo "Impossible de se connecter à la base de données";
    exit();
}

$action = $_GET['action'] ?? null;
$id = intval($_GET['id'] ?? 0);

$actionSuccess = 0;
if ($action === 'delete' && $id > 0) {
    $reqDelete = $db->prepare('DELETE FROM bien WHERE id_bien = :id');
    $success = $reqDelete->execute([
        ':id' => $id
    ]);

    $actionSuccess = ($success) ? 1 : 2;
}

if ($action === 'buy' && $id > 0) {
    $reqUpdate = $db->prepare('UPDATE bien SET disponible = 0 WHERE id_bien = :id');
    $success = $reqUpdate->execute([
        ':id' => $id
    ]);

    $actionSuccess = ($success) ? 1 : 2;
}

$reqType = $db->prepare(
    'SELECT
        id_bien, superficie, nbr_piece, adresse, departement,
        description_bien, prix, nom, prenom
    FROM profil
    INNER JOIN bien ON profil.id_profil = bien.id_agent
    WHERE `transaction` = :typeTransaction');

$reqType->execute([
    ':typeTransaction' => 0
]);
$locations = $reqType->fetchAll();

$reqType->execute([
    ':typeTransaction' => 1
]);
$ventes = $reqType->fetchAll();

?>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Accueil - Nom du site</title>
</head>
<body>
    <?php if ($actionSuccess === 1): ?>
        Action terminé !
    <?php elseif ($actionSuccess === 2): ?>
        Action impossible !
    <?php endif; ?>

    <h2>Liste des biens pour de la locations :</h2>

    <!-- Le CSS sert à styliser -->
    <table class="table-data">
        <thead>
            <tr>
                <th>Superficie maison</th>
                <th>Adresse</th>
                <th>Departement</th>
                <th>Nombre pieces</th>
                <th>Description bien</th>
                <th>Prix</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($locations as $location): ?>
                <tr>
                    <td><?= $location['superficie'] ?></td>
                    <td><?= $location['adresse'] ?></td>
                    <td><?= $location['departement'] ?></td>
                    <td><?= $location['nbr_piece'] ?></td>
                    <td><?= $location['description_bien'] ?></td>
                    <td><?= $location['prix'] ?></td>
                    <td>
                        <!--
                            Remplacer le <i class="fa fa-money"></i>
                            si tu n'a pas fontawesome
                        -->
                        <a href="agent.php?action=buy&amp;id=<?= $location['id_bien'] ?>"
                           class="btn btn-danger"><i class="fa fa-money"></i></a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <h2>Liste des biens pour de la ventes :</h2>

    <!-- Le CSS sert à styliser -->
    <table class="table-data">
        <thead>
            <tr>
                <th>Superficie maison</th>
                <th>Adresse</th>
                <th>Departement</th>
                <th>Nombre pieces</th>
                <th>Description bien</th>
                <th>Prix</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($ventes as $vente): ?>
                <tr>
                    <td><?= $vente['superficie'] ?></td>
                    <td><?= $vente['adresse'] ?></td>
                    <td><?= $vente['departement'] ?></td>
                    <td><?= $vente['nbr_piece'] ?></td>
                    <td><?= $vente['description_bien'] ?></td>
                    <td><?= $vente['prix'] ?></td>
                    <td>
                        <!--
                            Remplacer le <i class="fa fa-trash"></i>
                            si tu n'a pas fontawesome
                        -->
                        <a href="agent.php?action=delete&amp;id=<?= $vente['id_bien'] ?>"
                           class="btn btn-danger"><i class="fa fa-trash"></i></a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</body>
</html>

Car oui les fonctions mysql_ sont supprimé depuis php 7 soit depuis 3 ans si je dis pas de bêtise.