How to insert multiple inputs into the database using the power of PHP?

user3808774

I have a duplicate html form input now the problem here is I can't insert them all into the database. I was using foreach but the other fields cannot be inserted.

Here's my code:

<table><!---one name and date but has many products--->
    <tr>
            <td>Name</td>
            <td><input type="text" name="name_txt[]" /></td>
    </tr>
    <tr>
        <td>Date</td>
        <td><input type="date" name="date_txt[]" /></td>
    </tr>


    <tr>
        <td>Product</td>
        <td><input type="text" name="product_name_txt[]" /></td>
    </tr>
    <tr>
        <td>Description</td>
        <td><input type="text" name="description_txt[]" /></td>
    </tr>
    <tr>
        <td>Date Purchased</td>
        <td><input type="date" name="date_txt[]" /></td>
    </tr>
    <tr>
        <td>Quantity</td>
        <td><input type="text" name="quantity_txt[]" /></td>
    </tr>
    <tr>
        <td>Price</td>
        <td><input type="text" name="price_txt[]" /></td>
    </tr>

    <tr>
        <td><br><br></td>
    </tr>

    <tr>
        <td>Product</td>
        <td><input type="text" name="product_name_txt[]" /></td>
    </tr>
    <tr>
        <td>Description</td>
        <td><input type="text" name="description_txt[]" /></td>
    </tr>
    <tr>
        <td>Date Purchased</td>
        <td><input type="date" name="date_txt[]" /></td>
    </tr>
    <tr>
        <td>Quantity</td>
        <td><input type="text" name="quantity_txt[]" /></td>
    </tr>
    <tr>
        <td>Price</td>
        <td><input type="text" name="price_txt[]" /></td>
    </tr>

    <tr>
        <td><br><br></td>
    </tr>

    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submit" value="add" />
        <input type="reset" name="submit" value="clear" /></td>
    </tr>

</table>

Here's my php code for adding inserting into the database:

<?php   

if (isset($_POST['product_name_txt']))
    {      
        include 'db.php';

        foreach($_POST['product_name_txt'] as $row => $value){
                    $product=$_POST['product_name_txt'];
                    $description=$_POST['description_txt'];
                    $date=$_POST['date_txt'];                   
                    $quantity=$_POST['quantity_txt'];
                    $price=$_POST['price_txt'];

        $sql = "INSERT INTO products (product_name,description,date,quantity,price)
                    VALUES ('$value','','','','')";

            if (mysqli_query($conn, $sql)) {
                    echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }
        }
    }

?>
MS Thakur

It's easy, use foreach for take all data from post array.

if (isset($_POST['product_name_txt']))
{      
    include 'db.php';
    foreach($_POST['product_name_txt'] as $row => $value){          
                $product=$_POST['product_name_txt'][$row];
                $description=$_POST['description_txt'][$row];
                $date=$_POST['date_txt'][$row];         
                $quantity=$_POST['quantity_txt'][$row];
                $price=$_POST['price_txt'][$row];

    $sql = "INSERT INTO products (product_name,description,date,quantity,price)
                VALUES ('$value','','','','')";

        if (mysqli_query($conn, $sql)) {
                echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to insert multiple data to database using php?

From Dev

how to insert multiple checkbox values in single column in database using php

From Dev

how to insert multiple lines in json array in database using php

From Dev

Insert data from multiple array form inputs using php and mysql

From Dev

How to insert multiple rows at once without using inputs in CodeIgniter?

From Dev

How to insert multiple rows into database using hibernate?

From Dev

how to insert multiple array to database using codeigniter

From Dev

How to insert into database using AJAX, PHP and CodeIgniter?

From Dev

How to insert data into the database using mysqli in php

From Dev

How to Insert to database from loop Using PHP?

From Dev

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

From Dev

How to validate multiple inputs with PHP

From Dev

How to validate multiple inputs with PHP

From Dev

Insert multiple results into database PHP

From Dev

How to insert multiple, increasing by 1, rows to mysql database with php

From Java

How to insert multiple items into database when using Scrapy?

From Dev

How to insert multiple records to database using Laravel Eloquent

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

From Dev

Insert an Array into Database using PHP

From Dev

how to make an insert function with in a database class to insert a new record with multiple coloumns and multiple values (using PDO )?

From Dev

How to insert multiple data into database?

From Dev

How to insert values of dynamic fields into database using PHP ,MySqL and JQuery

From Dev

How to insert ö/ä/ü in Database using php utf8

From Dev

How to insert the hidden field data into database using php

From Dev

How to insert XML content into mysql database using PHP

From Dev

How to change format of date and insert it in database using php and html only

From Dev

matching multiple inputs with database

From Dev

How to get data from database and assign it to inputs values using PHP and jQuery AJAX

From Dev

how to separate the multiple inputs using space in console

Related Related

  1. 1

    How to insert multiple data to database using php?

  2. 2

    how to insert multiple checkbox values in single column in database using php

  3. 3

    how to insert multiple lines in json array in database using php

  4. 4

    Insert data from multiple array form inputs using php and mysql

  5. 5

    How to insert multiple rows at once without using inputs in CodeIgniter?

  6. 6

    How to insert multiple rows into database using hibernate?

  7. 7

    how to insert multiple array to database using codeigniter

  8. 8

    How to insert into database using AJAX, PHP and CodeIgniter?

  9. 9

    How to insert data into the database using mysqli in php

  10. 10

    How to Insert to database from loop Using PHP?

  11. 11

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

  12. 12

    How to validate multiple inputs with PHP

  13. 13

    How to validate multiple inputs with PHP

  14. 14

    Insert multiple results into database PHP

  15. 15

    How to insert multiple, increasing by 1, rows to mysql database with php

  16. 16

    How to insert multiple items into database when using Scrapy?

  17. 17

    How to insert multiple records to database using Laravel Eloquent

  18. 18

    how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

  19. 19

    Insert an Array into Database using PHP

  20. 20

    how to make an insert function with in a database class to insert a new record with multiple coloumns and multiple values (using PDO )?

  21. 21

    How to insert multiple data into database?

  22. 22

    How to insert values of dynamic fields into database using PHP ,MySqL and JQuery

  23. 23

    How to insert ö/ä/ü in Database using php utf8

  24. 24

    How to insert the hidden field data into database using php

  25. 25

    How to insert XML content into mysql database using PHP

  26. 26

    How to change format of date and insert it in database using php and html only

  27. 27

    matching multiple inputs with database

  28. 28

    How to get data from database and assign it to inputs values using PHP and jQuery AJAX

  29. 29

    how to separate the multiple inputs using space in console

HotTag

Archive