-1

我正在用 php 和 mysql 制作购物车。我在更新产品价格时遇到问题。我的代码如下。请告诉我我哪里错了。我的回声语句也不起作用,只显示带有产品价格的框。

<?php
include("includes/db.php");
include("includes/functions.php");

if(isset($_POST['update_price'])){
//print_r($_POST['price']);

    foreach($_POST['price'] as $priceup => $prvalue){ 

    echo $sql = "Update products SET price='$prvalue' where serial = '".     $_POST['product_data'][$priceup] ."'";
    echo $result=mysql_query($sql);}    }   



            ?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
         <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Products Prices Management</title>
                  <script language="javascript">
                        function update_cart(){
             document.form1.command.value='update';
                            document.form1.submit();
              }
                 </script>

          <style type="text/css">
             <!--
            .style3 {
        font-family: Georgia, "Times New Roman", Times, serif;
font-weight: bold;
font-style: italic;
color: #FF0000;
        }
        -->
            </style>
        </head>
       <body>
        <h1 class="style3">Products Price Management</h1>
         <table border="2" cellpadding="2px" width="473">
            <?php
        $result=mysql_query("select * from products");
        while($row=mysql_fetch_array($result)){
    ?>
            <tr>
         <input type="hidden" name="product_data[]"  value="<?php echo $row['serial']; ?>" />
         <td width="205"><b><?php echo $row['name']?></b></td>

        <td width="246"><input type="text" name="price[]" value="<?php echo  $row['price']?>"maxlength="3" size="2" />
             </td>

             </tr>
             <?php
                }
               ?>
           </table>
              <div>
    <input type="submit" name="update_price" value="Update Prices" >
           </div>

             </body>
            </html>

请告诉我我在哪里做错了?

4

2 回答 2

0

首先,您的表单没有提交。您忘记放置表单标签,请查看此处。

      <form  action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
      <table border="2" cellpadding="2px" width="473">
      <?php
      $result=mysql_query("select * from products");
       while($row=mysql_fetch_array($result)){
       ?>
      <tr>
      <input type="hidden" name="product_data[]"  value="<?php echo $row['serial']; ?>" />
      <td width="205"><b><?php echo $row['name']?></b></td>
      <td width="246"><input type="text" name="price[]" value="<?php echo  $row['price']?>"maxlength="3" size="2" />
         </td>
       </tr>
       <?php
        }
        ?>
        </table>
        <div>
       <input type="submit" name="update_price" value="Update Prices" >
       </div>
       </form>
于 2013-04-06T08:58:10.517 回答
0

在 foreach 语句中,它期望$_POST['price']是一个数组,尽管它不是。

于 2013-04-06T08:52:47.710 回答