How to insert multiple jquery on created rows values into database using php and mysql? -


here html code :

                    <tr>                         <td><input class="case" type="checkbox"/></td>                         <td><input type="text" data-type="productcode" name="itemno[]" id="itemno_1" class="form-control autocomplete_txt" autocomplete="off"></td>                         <td><input type="text" data-type="productname" name="itemname[]" id="itemname_1" class="form-control autocomplete_txt" autocomplete="off"></td>                         <td><input type="number" name="price[]" id="price_1" class="form-control changesno" autocomplete="off" onkeypress="return isnumeric(event);" ondrop="return false;" onpaste="return false;"></td>                         <td><input type="number" name="quantity[]" id="quantity_1" class="form-control changesno" autocomplete="off" onkeypress="return isnumeric(event);" ondrop="return false;" onpaste="return false;"></td>                         <td><input type="number" name="total[]" id="total_1" class="form-control totallineprice" autocomplete="off" onkeypress="return isnumeric(event);" ondrop="return false;" onpaste="return false;"></td>                     </tr>                 </tbody>             </table>         </div>     </div>     <div class='row'>         <div class='col-xs-12 col-sm-3 col-md-3 col-lg-3'>             <button class="btn btn-danger delete" type="button">- delete</button>             <button class="btn btn-success addmore" type="button">+ add more</button>         </div> 

php code:

if(isset($_post['form1'])) {    $statement = $db->prepare("insert table_products (itemno,itemname,price,quantity,total) values (?,?,?,?,?)");   $statement->execute(array($_post['itemno'],$_post['itemname'],$_post['price'],$_‌​post['quantity']);    $success_message = " inserted successfully."; } catch(exception $e) { $error_message = $e->getmessage(); }  }  

and here jquery code creats add more field :

var i=$('table tr').length; $(".addmore").on('click',function(){

html = '<tr>'; html += '<td><input class="case" type="checkbox"/></td>'; html += '<td><input type="text" data-type="productcode" name="itemno[]" id="itemno_'+i+'" class="form-control autocomplete_txt" autocomplete="off"></td>'; html += '<td><input type="text" data-type="productname" name="itemname[]" id="itemname_'+i+'" class="form-control autocomplete_txt" autocomplete="off"></td>'; html += '<td><input type="text" name="price[]" id="price_'+i+'" class="form-control changesno" autocomplete="off" onkeypress="return isnumeric(event);" ondrop="return false;" onpaste="return false;"></td>'; html += '<td><input type="text" name="quantity[]" id="quantity_'+i+'" class="form-control changesno" autocomplete="off" onkeypress="return isnumeric(event);" ondrop="return false;" onpaste="return false;"></td>'; html += '<td><input type="text" name="total[]" id="total_'+i+'" class="form-control totallineprice" autocomplete="off" onkeypress="return isnumeric(event);" ondrop="return false;" onpaste="return false;"></td>'; html += '</tr>'; $('table').append(html); i++; });

how can insert database 2 or 3 rows multiple values using php ?

count of field , try iterate using for loop of count , try store value using $i variable of for loop:

for($i=0;$i<count($_post['itemno']);$i++) {     $data['itemno']       = $_post['itemno'][$i];     $data['itemname']     = $_post['itemname'][$i];     $data['price']        = $_post['price'][$i];     $data['quantity']     = $_post['quantity'][$i];     $data['total']        = $_post['total'][$i];     //your insert query here } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -