0

Here is my 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 INTO 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 is my jquery code that 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 i insert database this 2 or 3 rows multiple values using php ?

9
  • sorry i don't understand what you want to see ? but please help me i am having a great problem last 5 days . Commented Jun 11, 2016 at 5:40
  • 1
    post php code which you have tried to store multiple row in db Commented Jun 11, 2016 at 5:41
  • It can store only one not multiple if(isset($_POST['form1'])) { $statement = $db->prepare("INSERT INTO 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(); } } Commented Jun 11, 2016 at 5:46
  • 1
    Please update your question with this information. Commented Jun 11, 2016 at 5:48
  • update with my php code Commented Jun 11, 2016 at 5:59

1 Answer 1

0

Count some of your field and try to iterate using for loop of that count and try to store its value by 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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank u so much ... It is so much helpful and i can successfully insert my multiple values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.