duanhuchang5056 2016-03-21 06:20
浏览 90
已采纳

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

I have a table in my database with a few fields. I am using this form to select data from this table:

<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>

I have to update the table using MySQL and PHP. How to do it? The screenshot of my web page is as below.

Screen Shot

When I click up update button the above rows should be updated. Please see the image for clear message.

  • 写回答

3条回答 默认 最新

  • dsgtew3241 2016-03-21 06:47
    关注

    Front view code(for display):

    <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>
    

    Back end code(For update):

    <?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");
    }
    ?>
    

    Warning: This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

    UPDATE:

    Used <input type='submit' name='submit' Value='Update' class="updateBtn"/> instead <button class="updateBtn"> Update </button>.

    This should work!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型