如何使用php使用更新按钮更新多个mysql字段

如何使用php使用更新按钮更新多个mysql字段,php,html,mysql,Php,Html,Mysql,我的数据库中有一个带有几个字段的表。我使用此表单从该表中选择数据: <form id="#" name="#" method= "post" action="cms_order_detail.php"> <table width="100%" border="1" align="center"> <th> Item </th> <th> Price </th> &

我的数据库中有一个带有几个字段的表。我使用此表单从该表中选择数据:

<form id="#" name="#"  method= "post" action="cms_order_detail.php"> 
    <table width="100%" border="1" align="center"> 
        <th> Item </th>
        <th> Price </th>
        <th> Active </th>
        <tr> 
        <?php 
        $sql= mysql_query("SELECT * FROM `product` where `item_id` = '$id' AND `type`=0 ");
        while($row= mysql_fetch_array($sql)){
            $price      =   $row['price'];
            $item_id    =   $row['item_id'];
            $size       =   $row['size'];
        ?>
            <td class="price"> <?php echo $size;?>  </td>
            <td class="price" width="1px"> 
                <input type="number" name="pro_price" id="pro_price" value="<?php echo $price;?>" style="width:4.2em; " > 
            </td>
            <td align="center">
                <input type="number" name="onof" id="onof" value="<?php echo $row['active'];?>" style="width:2.5em; " min="0" max="1" title="1= active and 0= De-active"> 
            </td>
        </tr>   
        <?php }?>
    </table> 
    <button class="updateBtn"> Update </button> 
    <div style="clear:both"> </div>
</form>

项目
价格
活跃的

为此,您必须在上述代码中进行更改,因为对于此代码,我们无法找到哪个值将与哪个行关联

 <?php 
     $sql= mysql_query("SELECT * FROM `product` where `item_id` = '$id' AND `type`=0 ");
     while($row= mysql_fetch_array($sql)){
         $price= $row['price'];
         $item_id=   $row['item_id'];    
         $size=  $row['size'];
?>
<td class="price"><?php echo $size;?>  </td>
<td class="price" width="1px"> 
    <input type="number" name="pro_price-$row['item_id']" id="pro_price-$row['item_id']" value="<?php echo $price;?>" style="width:4.2em; " > 
</td>
<td align="center">
    <input type="number" name="onof_$row['item_id']" id="onof_$row['item_id']" value="<?php echo $row['active'];?>" style="width:2.5em; " min="0" max="1" title="1= active and 0= De-active"> 
</td>
</tr>   
<?php }?>
前视图代码(用于显示):

<form id="frm_update" name="frm_update"  method= "post" action="cms_order_detail.php"> 
    <table width="100%" border="1" align="center"> 
        <th> Item </th>
        <th> Price </th>
        <th> Active </th>
        <tr> 
        <?php 
        $sql= mysql_query("SELECT * FROM `product` where `item_id` = '$id' AND `type`=0 ");
        while($row= mysql_fetch_array($sql)){
            $price      =   $row['price'];
            $item_id    =   $row['item_id'];
            $size       =   $row['size'];
        ?>
            <td class="price"> <?php echo $size;?>  </td>
            <td class="price" width="1px"> 
                <input type="number" name="pro_price[]" id="pro_price" value="<?php echo $price;?>" style="width:4.2em; " > 
            </td>
            <td align="center">
                <input type="number" name="onof[]" id="onof" value="<?php echo $row['active'];?>" style="width:2.5em; " min="0" max="1" title="1= active and 0= De-active"> 
            </td>
            <input type="hidden" name="item_id[]" value="<?php print($item_id);?>"/>
        </tr>   
        <?php }?>
    </table> 
    <input type='submit' name='submit' Value='Update' class="updateBtn"/>
    <div style="clear:both"> </div>
</form>
<?php
    /**
    * YOUR CONNECTION...
    *$conn = mysql_connect("localhost","root","");
    *mysql_select_db("phppot_examples",$conn);
    **/

    if(isset($_POST["submit"]) && $_POST["submit"]!="") {
        $item_idCount = count($_POST["item_id"]);
        for($i=0; $i<$item_idCount; $i++) {
            mysql_query("UPDATE users set price='" . $_POST["pro_price"][$i] . "', active='" . $_POST["onof"][$i] . "' WHERE item_id ='" . $_POST["item_id"][$i] . "'");
        }
    header("Location:MYFRONTURL.php");
}
?>

项目
价格
活跃的

你需要所有的身份证。您可以通过在foreach中发布以下命令来实现:

<input type="hidden" name="id[]" value="<?php echo $row['item_id']; ?>" />

这样,您就有了一次提交和多个更新。

您是否尝试过这样做?您是否也在使用JQuery AJAX或AJAX或简单PHP/MySQL?您在查询中使用了
item\u id='$id'
;您正在获取单个产品的详细信息吗?我在mysql中有一个类似这样的查询:(Select*form table where id=1;)并且针对id=1我有更多的4-5个字段,我通过(而($row=mysql\u fetch\u array query))打印上面的图像结果来获取这些字段。我想更新每一个值,如price、name、activefor update query,您可以尝试(update table set col='value',其中id=1)请查看。您仍然面临问题,请告知您面临的问题。对于此警告,您必须按照MySQLi转换代码。请查看老板,它不起作用,我不知道这有什么问题。@bc120200427SaleemJafar:请检查我的最新答案。谢谢。。!很乐意帮忙!现在,它被公认为“最佳答案”。我要求那个否决我答案的人保持中立。那太好了。谢谢
<input type="hidden" name="id[]" value="1" />
...
<input type="hidden" name="id[]" value="6" />
<input type="number" name="pro_price[<?php echo $row['item_id']; ?>]" />
<input type="number" name="onof[<?php echo $row['item_id']; ?>]" />
foreach($id as $id1)
{
// Pseudocode
UPDATE... SET pro_price=$pro_price[$id1],onof=$onof[$id1] WHERE id = $id1;
}