2013-04-06 87 views
-1

我在php & mysql中製作購物車。我有更新產品價格的問題。我的代碼在下面。 Plz告訴我我錯在哪裏。我的回聲聲明也不起作用,只顯示產品價格方塊。如何更新電子商品中的產品的價格

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

PLZ告訴我,我在哪裏做錯了?

+1

這是什麼錯誤? – 2013-04-06 08:50:43

回答

0

在foreach語句中,它預計$_POST['price']是一個數組,雖然它不是。

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> 
相關問題