2

I have a form where I can add items to the inventory. Currently, I can add only one item at a time. Here's the form screenshot: https://i.sstatic.net/1vHYr.png. But I want to accomplish this: https://i.sstatic.net/yGy8S.png (< just a sketch), which is adding multiple records to the database using one form. These are my current forms:

add.html:

<form action="add.php" method="POST">
    <select name="category" id="category">
      <option value="1">Caviar In Canned Jars</option>
    </select>
    <input id="name" name="name" type="text">
    <input id="size" name="size" type="text">
    <input id="sku" name="sku" type="text">
    <input id="price" name="price" type="text">
    <input value="Add" type="submit">
</form>

add.php:

$result = mysql_query("
    INSERT INTO `items` (name, size, sku, price, category_id)
    VALUES ('$_POST[name]', '$_POST[size]', '$_POST[sku]', '$_POST[price]', '$_POST[category]');");
    if (!$result) {
        echo "Something went wrong!";
    } else {
    echo '<script type="text/javascript">
                <!--
                    window.location = "add.html"
                //-->
          </script>';
}

How can I achieve the functionality in the second screenshot with PHP and SQL (https://i.sstatic.net/yGy8S.png)?

P.S.: I'm aware that it's prone to SQL injection, but it's only running on local web server

4
  • 4
    Do you want the entire HTML and PHP code, or can you do some of it yourself? Which parts do you need help with? Commented Oct 21, 2011 at 21:14
  • Only PHP and SQL, I can write the markup Commented Oct 21, 2011 at 21:17
  • 1
    @Timur Wether locally or public, it's not bad starting to develop certain habits...it's a few characters more, but won't let you down the day you make a public website and you forget about this things Commented Oct 21, 2011 at 21:24
  • I agree, good sir. But I was just only creating a dummy form to see how to implement it Commented Oct 21, 2011 at 21:26

1 Answer 1

3

Step 1. Change the html bits to be arrays of data by adding brackets after the input names.

<form action="add.php" method="POST">
    <!-- row 1 -->
    <select name="category[]" id="category">
      <option value="1">Caviar In Canned Jars</option>
    </select>
    <input id="name" name="name[]" type="text">
    <input id="size" name="size[]" type="text">
    <input id="sku" name="sku[]" type="text">
    <input id="price" name="price[]" type="text">
    <!-- row 2 -->
    <select name="category[]" id="category">
      <option value="1">Caviar In Canned Jars</option>
    </select>
    <input id="name" name="name[]" type="text">
    <input id="size" name="size[]" type="text">
    <input id="sku" name="sku[]" type="text">
    <input id="price" name="price[]" type="text">
    <!-- submit at bottom -->
    <input value="Add" type="submit">
</form>

There will be only one submit. And the post will hold all the data in arrays.

Step 2. Loop through the data either doing individual inserts or constructing a large query.

In this example there are 5 rows of data.

for($i = 0; $i < 5; $i++)
{
    $name = $_POST['name'][$i];
    $size = $_POST['size'][$i];
    ...

    /* verify data, do sql escaping */

    $result = mysql_query("
        INSERT INTO `items` (name, size, sku, price, category_id)
        VALUES ('$name', '$size', '$sku', '$price', '$category');
    ");

   /* do result handling */
}
Sign up to request clarification or add additional context in comments.

Comments

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.