• First of all you can check https://prnt.sc/O7dfX2q-pHYB.I am trying to develop like this. But Multiple rows data doesn’t insert in the custom table only one row insert i used Ajax for data insert.I know there was simple issues.But I cant find out the issues. I used multiple insert script. Please help me to solve the issues.

    Insert Code -1

    for ($a = 0; $a < count($_POST['product']); $a++)
          { 
          $table_name = $wpdb->prefix . 'estimate_details';
          
            $data= array(  
              'itr_estimate_nbr' => sanitize_text_field($_POST['estmateno']), 
              'itr_pro_name' => sanitize_text_field($_POST['product'][$a]), 
              'itr_description' => sanitize_text_field($_POST['description'][$a]), 
              'itr_qtn'  => sanitize_text_field($_POST['qty'][$a]),
              'itr_rate'    =>  sanitize_text_field($_POST['price'][$a]),
              'itr_rate_total'     =>  sanitize_text_field($_POST['total'][$a])
            );
    
            $format = array(
                    '%d',
                    '%s',
                    '%s',
                    '%d',
                    '%d',
                    '%f'
                 );
                 
                 $wpdb->insert($table_name,$data,$format);
            }
    

    code insert-2

    $table_name = $wpdb->prefix . 'estimate_details';
    
        for ($a = 0; $a < count($_POST['product']); $a++) {
        $wpdb->insert(
            $table_name,
            array(
                'itr_estimate_nbr' => $wpdb->prepare( '%d', $_POST['estmateno'] ),
                'itr_pro_name' => $wpdb->prepare( '%s', $_POST['product'][$a] ),
                'itr_description'=> $wpdb->prepare( '%s', $_POST['description'][$a] ),
                'itr_qtn'=> $wpdb->prepare( '%d', $_POST['qty'][$a] ),
                'itr_rate'=> $wpdb->prepare( '%d', $_POST['price'][$a] ),
                'itr_rate_total'=> $wpdb->prepare( '%f', $_POST['total'][$a] ),
            )
         );
      }
    

    Ajax script

    jQuery(document).on('click', '#addInvoice', function () { 
    
       var product = document.getElementById('product').value;
            var description = document.getElementById('description').value;
            var qty = document.getElementById('qty').value;
            var price = document.getElementById('price').value;
            var total = document.getElementById('total').value;        
              
        jQuery.ajax({
            url: ajax_url,
            type: "POST",
        cache: false,
            data: {
                'action':'new_db_estimate_info',
    
                'product' : product,
                'description' : description,
                'qty' : qty,
                'price' : price,
                'total' : total,
                            
            },
            success:function(data) {
              
                  jQuery('body #message').html('Customer created successfully');
                if(data=='Message sent successfully'){
                   jQuery('#message').text(data.message);   
                   jQuery("form").trigger("reset");
     
                }else{
                    jQuery('#survey_error_message').text(data);
                    jQuery("form").trigger("reset");
                }
            
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
    
          });  
        });
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    What is the product field’s name attribute? I assume this is a multi-select field or a checkbox group. The name must be suffixed with [] so that an array of selections is passed to PHP, for example name="product[]". Without it, PHP is only passed the last selection.

    Thread Starter Rasheduzzaman khan

    (@khan9)

    Product attribute name is name="product[]" I have already use row php code by this data inserted successfully. Please check the following code. But while i used in the wordpress that is not working perfectly.

    for ($a = 0; $a < count($_POST["product"]); $a++)
        {
          $sql = "INSERT INTO items (invoiceId,itemName,itemdescription,itemqty,itemrate,itemAmount) VALUES ('$invoiceId', '" . $_POST["product"][$a] . "', '" . $_POST["description"][$a] . "'
            ,'" . $_POST["qty"][$a] . "','" . $_POST["price"][$a] . "','" . $_POST["total"][$a] . "')";
    
          mysqli_query($conn, $sql);
        }
         

    Moderator bcworkz

    (@bcworkz)

    Nothing jumps out at me as being wrong. You’ll need to do your own debugging to find out where things are going wrong. Debugging Ajax is a little tricky since you cannot simply var_dump() values to see what’s going on. You need to eliminate the Ajax aspect and debug within WP. One way to do so is to create a custom page template that directly calls your Ajax action callback. Your template code will need to define the expected data and assign it to $_POST. Then you can var_dump() different variables at strategic places to see where things go wrong.

    You should also confirm the form data is coming in correctly from your jQuery Ajax call. Add temporary code to your action callback that outputs $_POST to a log file where you can easily examine the content. The sent content can then serve to help you setup test data on the page template.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘jQuyer invoice doesn’t insert in WordPress custom table’ is closed to new replies.