1

Aim

Delete all data where the ID is selected

Problem

The data is not deleted

Error

No Errors was prompted within the developer console

Work Description

Based on what I understand the codes function by retrieving the User ID (delete_userID) and then passing it into $delete_userID by using this line mysqli_real_escape_string($conn,$_POST['delete_userID']);. But I can't understand why doesn't the delete query work. I have viewed several online question regarding this and some of it was because of the missing database code which is mysql_select_db("data2017", $conn); but that does not seem to be the problem for me. Also I set it as once the input button is clicked, it will send a "delete" call and the if statement is to checked if the button has been clicked and the call has been received. In addition to retrieving the User ID, the User ID that is to be deleted was passed in through the use of the JavaScript function as shown below.

Delete Codes

<div class="modal-body">
  <p>Do you want to delete?</p>
</div>
<div class="form-group">
  <label for="delete_userID" class="control-label">User ID:</label>
  <input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
</div>
<div class="modal-footer">
  <input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
    <?php
        if(isset($_POST['delete'])){
            $delete_userID=mysqli_real_escape_string($conn,$_POST['delete_userID']);
            mysql_select_db("data2017", $conn);
            $sql = "DELETE FROM pha_user WHERE id ='".$delete_userID."'";
            $Deletequery=mysqli_query($conn,$sql);
            if($Deletequery){
                echo "<script>alert('Delete Successful.');</script>";
            }
            else{
                echo "<script>alert('Delete Failed.');</script>";
            }
        }
    ?>
</div>

UPDATE

The delete_userID was passed in through a JavaScript function as shown below.

function bindList() {
    var $table = $('#eventsTable');
    $table.bootstrapTable({
        url: 'list-phaUser.php',
        search: true,
        pagination: true,
        columns: [{
            field: 'userID',
            title: 'User ID',
            sortable: true,
            },{
            field: 'operate',
            title: 'Action',
            align: 'center',
            sortable: true,
            events: operateEvents,
            formatter: operateFormatter
        },],
    });
    $(".fixed-table-toolbar").append("<div id='table-title' class='columns columns-left btn-group pull-left'>User List</div>");
    $("#divFooter").remove();
    $("<div class='row' id='divFooter' style='margin-left:-4%;margin-right:-4%;margin-bottom:-2%;'></div>").insertAfter("#divtb1");
    $("#divFooter").load('footer.php');
}

    window.operateEvents = {
    'click .icon_edit': function (e, value, row, index) {
        $("#edit_userID").val(row.userID);
        $('#edit_model').modal('show');
    },
    'click .icon_delete': function (e, value, row, index) {
        $("#delete_userID").val(row.userID);
        $('#delete_model').modal('show');
    }
};
4
  • 1
    Can we please see more of the <form> element Commented Dec 27, 2017 at 9:58
  • 2
    there are no value inside delete_userID. Commented Dec 27, 2017 at 9:59
  • @Scuzzy I didn't put a <form> inside and I realized that was my problem Commented Dec 27, 2017 at 10:35
  • @saddam The value was passed into by the javascript function. I'll edit my question. But Riccardo pointed out that I did not have the <form> and that was my problem. Commented Dec 27, 2017 at 10:37

2 Answers 2

3

You didn't make a form.

<div class="modal-body">
    <p>Do you want to delete?</p>
</div>
<form method="POST" action="">
    <div class="form-group">
        <label for="delete_userID" class="control-label">User ID:</label>
        <input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
    </div>
    <div class="modal-footer">
        <input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
    </div>
</form>
<?php
if (isset($_POST['delete'])) {
    $delete_userID = mysqli_real_escape_string($conn,$_POST['delete_userID']);
    mysql_select_db("data2017", $conn);
    $sql = "DELETE FROM pha_user WHERE id ='".$delete_userID."'";
    $Deletequery = mysqli_query($conn,$sql);
    if ($Deletequery) {
        echo "<script>alert('Delete Successful.');</script>";
    } else {
        echo "<script>alert('Delete Failed.');</script>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Riccardo!!! I just tried that and it worked!! =D Merry Christmas and Happy New Year
:) Happy Christmas to you too.
1

Try with PDO, because of latest PHP versions deprecated MySQL DB connection. First, you have to declare the form method and action then you have to close it also.

Try the below code.

<?php
$config = array(
 'host'     => 'localhost',
 'username' => 'db_username',
 'password' => 'db_password',
 'dbname'   => 'data2017'
);

$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

 $delete_userID = $_POST['delete_userID'];

 // No need to use mysqli_real_escape_string when we using prepared statements

 $stmt = $db->prepare("DELETE FROM pha_user WHERE id = '$delete_userID' ");
 $stmt->execute();

 if($stmt){
    header("Location:appropriate_page.php?del_success");
 }else{
    header("Location:appropriate_page.php?del_failure");
 }
}
?>

<form method="POST" action="">
<div class="form-group">
  <label for="delete_userID" class="control-label">User ID:</label>
  <input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
</div>
<div class="modal-footer">
  <input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
</div>
</form>

3 Comments

Thank you for helping out, I will have to try that when I get back to work. Merry Christmas and Happy New Year :)
No mention. Use PDO bcz now a days servers also repeatedly prompts like upgrade your php version.
Noted and Thanks for your advice. I'm very new to PHP, I'll be reading up on that =D

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.