skip to Main Content

I wrote the following script for the delete customer information using jquery and Ajax.when i click the delete button console is showing POST http://localhost:10017/wp-admin/admin-ajax.php 500 (Internal Server Error), But when I use this script SQL command from phpmyadmin. that is working. Could yo tell me how to fix it and where is problem.

**php script**

public function delete_customer_data() {
     global $wpdb;
     $id = $_POST['id']; 
     

    $delcustomer = $wpdb->delete("

    DELETE wp_customer_info, wp_estimate_details, wp_estimate_inv_info, wp_estimate_sub_total,
    wp_invoice_details, wp_invoice_info, wp_invoice_sub_total, wp_payment_recieve 
    FROM wp_customer_info
    LEFT JOIN wp_estimate_details ON wp_customer_info.id = wp_estimate_details.customer_id
    LEFT JOIN wp_estimate_inv_info ON wp_customer_info.id = wp_estimate_inv_info.customer_id
    LEFT JOIN wp_estimate_sub_total ON wp_customer_info.id = wp_estimate_sub_total.customer_id 

    LEFT JOIN wp_invoice_details ON wp_customer_info.id = wp_invoice_details.itr_inv_customer_id 
    LEFT JOIN wp_invoice_info ON wp_customer_info.id = wp_invoice_info.itr_inv_cust_id 
    LEFT JOIN wp_invoice_sub_total ON wp_customer_info.id = wp_invoice_sub_total.itr_inv_cust_id 
  
    LEFT JOIN wp_payment_recieve ON wp_customer_info.id = wp_payment_recieve.itr_pay_cust_id   

    WHERE wp_customer_info.id ='".$id."'
    ");
      
    echo wp_json_encode($delcustomer);

    wp_die();
   }

**Ajax script**

jQuery(document).on('click', '#custdel', function() {

   var id = $(this).data('id');
    console.log(id);
    jQuery.ajax({
      type: 'POST',
      url: ajaxurl, 
      dataType: 'json',
      cache: false,
      data: {
        action: 'delete_customer_data', 
        id: id 
      },
      success: function(response) {
        //console.log('333');
        itr_customer_list_show();
        console.log('Data deleted successfully!');
      },
      error: function(xhr, status, error) {
        console.log('Error deleting data: ' + error);
      }
    });
  });

2

Answers


  1. Chosen as BEST ANSWER

    This the correct php script

    $wpdb->query(
          $wpdb->prepare(
            "DELETE wp_customer_info, wp_estimate_details, wp_estimate_inv_info, wp_estimate_sub_total, wp_invoice_details, wp_invoice_info, wp_invoice_sub_total, wp_payment_recieve
            FROM wp_customer_info
            LEFT JOIN wp_estimate_details ON wp_customer_info.id = wp_estimate_details.customer_id
            LEFT JOIN wp_estimate_inv_info ON wp_customer_info.id = wp_estimate_inv_info.customer_id
            LEFT JOIN wp_estimate_sub_total ON wp_customer_info.id = wp_estimate_sub_total.customer_id
            LEFT JOIN wp_invoice_details ON wp_customer_info.id = wp_invoice_details.itr_inv_customer_id
            LEFT JOIN wp_invoice_info ON wp_customer_info.id = wp_invoice_info.itr_inv_cust_id
            LEFT JOIN wp_invoice_sub_total ON wp_customer_info.id = wp_invoice_sub_total.itr_inv_cust_id
            LEFT JOIN wp_payment_recieve ON wp_customer_info.id = wp_payment_recieve.itr_pay_cust_id
            WHERE wp_customer_info.id = %d",
            $id
          )
        );
    

  2. Please, use $wpdb->query( $your_sql ); for this case

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search