2013-07-13 34 views
0

我試圖設置一個訂單,客戶輸入任何數量,因此我的庫存得到更新,從而保持下一個客戶的當前金額,我一直在想出正確的代碼但到目前爲止沒有運氣。任何意見將不勝感激,這裏是我的代碼:更新數據表中的數量

<?php 
session_start(); //Lets start a session! This is the index page 
$_SESSION["access"] = "My_session"; 
//We need a header right, well here it is? 
define ("TITLE", "Products page"); 
include('templates/header.html'); 
?> 
<h2 class="intro"> 
    Looking for the perfect widget for the perfect person? Browse through our small but original 
    list of products to purchase. Alternatively you can contact us through our contact page and one of our team will get 
    back to you as soon as possible. 
    WIDGET ORDER FORM Widget Quantity Cost Dehydrated water " /> $ 18.50 Glass Stems " /> $ 28.50 

    <?php 
    // Get the values from the $_POST array: 
    $quantityarray = ((isset($_POST['f_wa']) ? $_POST['f_wa'] : '') || (isset($_POST['f_gl']) ? $_POST['f_gl'] : '')); 

    include 'connection.php'; 

    # Define the SQL statement 
    $query = 'Select ProductID, InStock from Inventory ORDER BY ProductID'; 
    if ($result = mysql_query($query)) { 
     while ($row = mysql_fetch_array($result)) { 
      $currentnum = $row['InStock'] - $quantityarray[$row['ProductID']]; 
      $prodid = $row['ProductID']; 
      // execute a SQL statement to update the InStock number 
      $query = "UPDATE Inventory SET InStock = $currentnum WHERE ProductID=$prodid"; 
      sendquery($query); 
     } 
    } 

    function sendquery($query) 
    { 
     if (@mysql_query($query)) { 
      print '<p>Table populated.</p>'; 
     } else { 
      print '<p style="color:red;">Error executing SQL statement: <br/>' . mysql_error() . '.</p> 

    <p>The statement being run was: ' . $query . '</p>'; 
     } 
    } 

    ?> 
    <?php 
    //To conclude we also need the footer, don't we?. 
    include('templates/footer.html'); 
    ?> 

回答

0

你永遠不會得到$productquantity陣列。您的發言將評估爲true或false:

$quantityarray = ((isset($_POST['f_wa']) ? $_POST['f_wa'] : '') || (isset($_POST['f_gl']) ? $_POST['f_gl'] : ''));            

||在此聲明意味着,如果第一個語句或第二條語句爲真,則設置$quantityarray到別的真設置$quantityarray爲false

你應該var_dump($quantityarray)到看到這個。