The Employee 123
The Employee 123

Reputation: 504

PHP - SQL - DELETE Query does not function

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');
    }
};

Upvotes: 1

Views: 578

Answers (2)

Riccardo Zunino
Riccardo Zunino

Reputation: 83

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>";
    }
}
?>

Upvotes: 3

Naveen Kumar E
Naveen Kumar E

Reputation: 13

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>

Upvotes: 1

Related Questions