Pergunta

I'm trying to build an inventory system to keep track of some products in my company. It isn't an online store or anything, just a way for me to keep track of what products are in shipping, in an electrician's truck stock, or in the office.

I've already built pages to show current inventory levels of what's in limbo (shipping to a tech or the office), what techs have, and what is here in the office. I can also see what stock each tech has also. Now I just need a way to assign products to one of these locations when they are drop shipped by our supplier.

The Insert Inventory page has a form that displays fields for every item we keep track of, and a quantity box next to it. It also has a select box for choosing what to assign the inventory to and where it is intended to go next. (For example: Shipping -> Tech)

So, it might look like this:


Choose Location Type Select List

Choose Going To Select List

12/2 Indoor Wire (Read Only)............Quantity Field

10/2 Outdoor Cable (Read Only)..............Quantity Field

Submit Button


There is actually going to be about 60 products on this page. Ideally, if they put 10 in the 12/2 wire field, it'll insert into the database 10 records that have:

id(AI), product_id, location_type_id, location_going_id, date

And if I put 3 on 10/2 quantity then it will insert 3 records of that also.

Here is the code for my form. Right now there's not much else.

      <form action="insert2.php" id="form1" name="form1" method="POST">
  <table width="500" border="0">
    <tr>
      <td><input name="product_name[]" type="text" value="10/2 Outdoor Cable" readonly="readonly"></td>
      <td><input name="qty[]" type="text" size="5"></td>
    </tr>
    <tr>
      <td><input name="product_name[]" type="text" value="12/2 Indoor Wire" readonly id="product_name[]"></td>
      <td><input name="qty[]" type="text" size="5" id="qty[]"></td>
    </tr>
  </table>

    <p>
      <input type="submit" name="submit" id="submit" value="Submit">
              </p>
</form>

And then I have it going to the second page to insert it into the database:

<?php
for($i = 0; $i < count($_POST['product_name']); $i++) {
$product = $_POST['product_name'][$i];
for ($j = 0; $j < $_POST['qty'][$i]; $j++) {

mysqli_query($con,"INSERT INTO inv_inventory (product_name)
VALUES ('$product')");
}

}
?>

When I get to the second page, it shows: string(16) "12/2 Indoor Wire" and it inserted 1 of the 10/2 outdoor cable items. I put 3 for outdoor and 4 for indoor products.

Foi útil?

Solução

You can change your HTML to

<input name="product_name[]" .. />

And then you will be able to manipulate $_POST["product_name"] as an array, containing all posted values.

You need to do the above for both the product_name input, and the qty input, so that you can do any necessary actions.

If you want to insert multiple records based on your quantity, your code must look something like:

<?php
  for($i = 0; $i < count($_POST['product_name']); $i++) {
    $product = $_POST['product_name'][$i];
    for ($j = 0; $j < $_POST['qty'][$i]; $j++) {
      // insert $product
    }
  }
?>

If your quantity is just another column in the product table, then your code should look like:

<?php
  for($i = 0; $i < count($_POST['product_name']); $i++) {
    $product = $_POST['product_name'][$i];
    $query = 'INSERT INTO .. (`product_name`, `qty`) '.
             ' VALUES ("'.$product.'", "'.$_POST['qty'][$i].'")';
  }
?>

Keep in mind that this is just an example. You need to check and escape values in real code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top