-1

I have a form which I use to update stock levels in a mysql table. The three fields I post are:

lst_product_id[]
txt_quantity[]
txt_cost[]

I can do this easily enough to update one product, however, I would like to be able to update more than one product at a time from the form by adding some more input fields.

To do this, I added another row of three input fields (same as above) and put the [] square brackets behind all six to create an array.

This is where it all goes pear shaped, can anyone show me how to pick up the array when I post to the insert page and and update the table.

I am using the mysqli extension and am really lost here. Thank's in advance. David

1
  • I don't have an code to show as I am stumped,i think I need to kick off with something along the lines of foreach($_POST['lst_product_id'] as $value)then somehow assign the next field to the array etc.Then I suspect I would need to count the amount of records loop to update.I could do with a simple example Commented Mar 12, 2013 at 20:38

1 Answer 1

0

You're on the right track. Here is a simple example as requested that will hopefully help make the connection...

<?php
$post = $_POST;
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<form method="post">
    <input name="id[1]" value="1" />
    <input name="quantity[1]" value="2" />
    <input name="cost[1]" value="100" />
    <input name="id[3]" value="3" />
    <input name="quantity[3]" value="1" />
    <input name="cost[3]" value="250" />
    <input name="id[8]" value="8" />
    <input name="quantity[8]" value="12" />
    <input name="cost[8]" value="25" />
    <br />
    <input type="submit" />
</form>
<pre>
<?php print_r($post); ?>
</pre>
</body>
</html>

After you submit the form, you will see the format of the post data you have to work with. The value put in square brackets [] should be the id so that you can match up the cost and quantity correctly. Something like so...

foreach ($post['id'] as $id) {
    $cost = $post['cost'][$id];
    $quantity = $post['quantity'][$id];

    //do something with each
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank's Marc, I will have a go at this and let you know how I get on,I'm going to stay at this so that I can get a good understanding of arrays. David

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.