marly
marly

Reputation: 1

execute mysql DELETE query on click

i'm kind of a new player in php and sql field. i'm trying to delete identity from my persons table when clicking on the remove link (or button) can somebody tell me what am i doing wrong?

this is my php code:

<?php
    $db = new DB();
    $cg_id = $_SESSION['cg_id'];
    $cg_address_id = $_SESSION['cg_address_id'];
    $sql ="SELECT f_name, phone, c.id as idc
           FROM contacts as c 
           WHERE c.cg_id = '$cg_id'";

    $result = $db->mysqli->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "<article class='contactArea'>";
            echo  "<a href='contacts2.php?del=".$row["idc"]."' class='deleteContact' name='remove' value='remove'>Remove</a></article>";    

            if(isset($_POST['idc'])){

            $idco = $_POST['idc'];

            $removeQuery = "DELETE FROM contacts as c WHERE id=".$idco." ";


            $resultt = mysql_query($removeQuery);
            if($resultt) {
            header('Location: '.$_SERVER['REQUEST_URI']);
            }
            echo "<script>window.location.reload(true);</script>";
        }
        }

    }else {
        echo "Please edit senior profile for monitoring!";
    }
?>

Upvotes: 0

Views: 1548

Answers (1)

SteJ
SteJ

Reputation: 1531

Try this (obviously replacing "localhost", "dbuser", "dbpassword" and "database_name" with the details for your mysql server and database):

<?php
                    $db = new mysqli("localhost","dbuser","dbpassword","database_name");
                    $cg_id = $_SESSION['cg_id'];
                    $cg_address_id = $_SESSION['cg_address_id'];
                    // I've moved the deletion code to BEFORE the select query, otherwise the
                    // query will be shown including the to-be-deleted data and it is then deleted after it is displayed
                    if(isset($_GET["del"])){ // <--- this was $_POST["del"] which would have been unset
                        $idc = $_GET["del"];
                        if($db->query("DELETE FROM contacts WHERE id=$idc")){
                             echo "deleted";
                        } else { 
                            echo "fail";
                        }    
                    }  
                    $sql ="SELECT photo, f_name, phone, street, street_num, city, l_name, c.id as idc FROM contacts as c, address as a WHERE c.cg_id = '$cg_id' and a.id = c.address_id";
                    $result = $db->query($sql);
                    if ($result->num_rows > 0) {
                        // output data of each row
                        while($row = mysqli_fetch_assoc($result)) {
                            echo "<article class='contactArea'>";
                            echo "<article class='contact5 lior'>";
                            echo "<img class='CSImage' src='" .$row["photo"]."'>";
                            echo "<section class='generalFormTextW nameCPosition'> " .$row["f_name"]." ".$row["l_name"]."<br></section>";
                            echo "<section class='generalFormTextW phoneCPosition'> " .$row["phone"]."<br></section>";
                            echo "<section class='generalFormTextB addressCPosition'>".$row["city"].", <br> ".$row["street"]." ".$row["street_num"]. "<br></section>";
                            echo  "<a href='contacts2.php?del=".$row["idc"]."' class='deleteContact' name='remove' value='remove'>Remove</a></article></article>";       
                        }
                    }
?>

Notice that I'm changing the way you're using mysqli so that you are using it directly rather than as a member of the DB object which is the way I've seen it used elsewhere - It looks to me as if you don't actually open the database connection (although maybe you just didn't include it because it showed your password?)

**EDIT: I've changed $_POST["del"] to $_GET["del"] -- because you are setting del in a url ("contacts2.php?del=") this will be GET not POST.

**EDIT: I've moved the deletion code so that it fixes the problem where you have to refresh the page to see the data with the record deleted - previously the information was shown and THEN deleted, we want to delete THEN show.

Upvotes: 1

Related Questions