从表单发布数组以更新mysql表 [英] Post array from form to update mysql table

查看:73
本文介绍了从表单发布数组以更新mysql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用来更新mysql表中库存水平的表格。我发布的三个字段是:

  lst_product_id [] 
txt_quantity []
txt_cost []

我可以很容易地做到这一点来更新一种产品,但是,我希望能够更新更多



为此,我在表单中一次添加了一个产品。



为此,我添加了另一行三个输入字段(与上面相同),然后将[]六个括号后面都创建了一个数组。



这就是所有梨形的地方,当我张贴到插入页面并更新表。



我正在使用mysqli扩展名,在这里确实迷路了。提前致谢。
David

解决方案

您在正确的轨道上。以下是所请求的一个简单示例,希望可以帮助建立连接...

 <?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>
<输入名称= id [1]值= 1 />
<输入名称= quantity [1] value = 2 />
<输入名称= cost [1] value = 100 />
<输入名称= id [3]值= 3 />
<输入名称= quantity [3] value = 1 />
<输入名称= cost [3] value = 250 />
<输入名称= id [8]值= 8 />
<输入名称= quantity [8] value = 12 />
<输入名称= cost [8] value = 25 />
< br />
< input type = submit />
< / form>
< pre>
<?php print_r($ post); ?>
< / pre>
< / body>
< / html>

提交表单后,您将看到必须使用的帖子数据的格式。方括号[]中的值应为ID,以便您可以正确匹配成本和数量。像这样...

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

//对每个
都做某事}


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

解决方案

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
}

这篇关于从表单发布数组以更新mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆