0

I dont know whats wrong with my code but i have this error Notice: Undefined index: delete

I want to make delete function with CRUD

here is my php code:

    if($_POST["delete"])
{
    $query = '
    DELETE FROM `users` WHERE id = :id
    ';
    $statement = $connect->prepare($query);
    $statement->execute(
        array(
            ':id'              => $_POST['id']
        )
    );
    $result = $statement->fetchAll();
    if(isset($result))
    {
        echo '<div class="alert alert-fill-danger" role="alert">User Deleted!<div>';
    }
}

and here is the AJAX function

$(document).on('click', '#delete', function(){
       var id = $(this).data('id');
       $('#message').html('');

       Swal.fire({
  title: 'Are you sure?',
  text: "You want to delete this user?",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes'
  }).then((result) => {
       if (result.value){
        $.ajax({
         url:'/auth/action',
         method:'POST',
         data:{id:id},
         success:function(data)
         {
          if(data != '')
          {
           load_user_data();
           $('#message').html(data);
          }
         }
        });
                      Swal.fire(
      'User Deleted!',
      '',
      'success'
    )
       }
       })

      });

I dont know what i missed here but the function in php is not working??

Jay Jay
  • 9
  • 2
  • the ajax function only sends the id `data:{id:id}` - change to `data:{id:id, delete:true}` etc – Professor Abronsius Jul 31 '19 at 06:38
  • @RamRaider i changed it still i got the problem in my PHP code what do you think is my wrong there? – Jay Jay Jul 31 '19 at 06:40
  • same error? or has the error changed now to something else? Also change `if( $_POST["delete"] ){` to `if( isset( $_POST['delete'] ) {` – Professor Abronsius Jul 31 '19 at 06:41
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – misorude Jul 31 '19 at 06:42
  • in php page you have checked delete in post i.e if($_POST["delete"]).Instead you can check if($_POST["id"]) which you are sending via ajax.Thanks – Ritesh Dhoke Jul 31 '19 at 06:45
  • @RamRaider no error affter changing to isset but the problem now is the user is not being deleted – Jay Jay Jul 31 '19 at 06:46

2 Answers2

0

Perhaps try the following: Test for the availability of the variables before trying to use them in PHP. Also - there will be no recordset so fetchAll will not work.

if( isset( $_POST['delete'], $_POST['id'] ) {
    ob_clean();
    $id=$_POST['id'];

    $query = 'DELETE FROM `users` WHERE id = :id';
    $statement = $connect->prepare( $query );
    $result = $statement->execute( array( ':id' => $id ) );

    if( $result && $statement->rowCount() > 0 ){
        echo '<div class="alert alert-fill-danger" role="alert">User Deleted!<div>';
    }
    exit();
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

here is the answer to my problem :D

var action = 'delete';

I just add this code under the id in ajax

if($_POST["action"] == 'delete')
    {
        $query = '
        DELETE FROM `users` WHERE id = :id
        ';
        $statement = $connect->prepare($query);
        $statement->execute(
            array(
                ':id'              => $_POST['id']
            )
        );
        $result = $statement->fetchAll();
        if(isset($result))
        {
            echo '<div class="alert alert-fill-danger" role="alert">User Deleted!<div>';
        }
    }
Jay Jay
  • 9
  • 2