0

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!";
    }
?>
4
  • 2
    What result/error do you get and what result you would like to receive? Commented Jul 28, 2015 at 10:57
  • 1
    you appear to be mixing mysql and mysqli commands and the link you generate for deleting the link uses 'del' as the parameter in a querystring yet the code looks for a POSTed variable called 'idc' Commented Jul 28, 2015 at 10:59
  • and also u may need to place session_start(); before using session Commented Jul 28, 2015 at 11:02
  • Where is your POST form? Commented Jul 28, 2015 at 11:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

i opened session in another page (db.php), otherwise how can i see all my contacts from db? this query works, but the remove query still not. i've tried to change to your way but still no difference....i'm clicking on the remove button and nothing happen.
I've just spotted the problem - See the edit - You had $_POST where you should have used $_GET as del= is set in the URL rather than a POSTed submission -- I'm somewhat embarassed I didn't spot it sooner!
another small thing - you have any idea why i need to press twice on the delete button? i know that it's because of the reload(), but i can't use it here under the isset cause it stock in the loop.........
I've just edited again - Moving the deletion code as mentioned in the edit should fix the "click twice" problem.
Also just a final comment: the "if ($result->num_rows > 0)" test isn't necessary - while($result->fetch_assoc()) will not run the code in the loop if there are no records.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.