2012-02-15 243 views
0

當我更改產品數量並更新數據時,它會丟失,而update命令實際上會將總列設置爲0(實際上更新代碼工作正常,直到我編輯了表單頁面包括在底部總,下面是屏幕截圖,以及更新代碼更新數據庫

form

enter image description here

形式腳本

<?php require_once("include/session.php");?> 
<?php require_once("include/dataconnect.php");?> 
<?php require_once("include/functions.php");?> 
<?php include("include/basicheader.php");?> 
<div class="y-acc-contain"> 
<div class="page-top"> 
<table width="100% class="y-acc-pagehead"> 
<tbody> 
<tr> 
<td> 
<h1> 
"Your account"<?php echo $_SESSION['username'];?> 
</h1> 
</td> 
</tr> 
</tbody> 
</table> 
</div> 
<div class="column-holder"> 
<div class="primary"> 
<div class="action-box rounded"> 
<div class="titlebar"> 
<h2>Order Form</h2> 
</div> 
<div class="listbar"> 
<div> 
<?php 
$submit = $_POST['Add']; 

//form data 
$Sname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Sname']))); 
$Pname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pname']))); 
$Pidno = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pidno']))); 
$Psize = mysql_real_escape_string(htmlentities(strip_tags($_POST['Psize']))); 
$Pcolour = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pcolour']))); 
$Pquantity = $_POST['Pquantity']; 
$Weblink = mysql_real_escape_string(htmlentities(strip_tags($_POST['Weblink']))); 
$Price = mysql_real_escape_string(htmlentities(strip_tags($_POST['Price']))); 
$date = date("Y-m-d"); 


if('POST' === $_SERVER['REQUEST_METHOD']) 

{ 
if ($Sname&&$Pname&&$Pidno&&$Weblink&&$Price) 
{ 
if (is_numeric($Price)) 
{ 
    $repeatheck = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}' AND Pidno ='$Pidno' AND Sname='$Sname'"); 
    $count = mysql_num_rows($repeatheck); 
if($count!=0) 
{ 
    die ('PRODUCT ALREADY IN BASKET YOU CAN INCREASE OR DECREASE QUANTITY'); 
} 
else 
$tprice = $Price * $Pquantity; 
$queryreg = mysql_query(" 
INSERT INTO repplac VALUES ('','$Sname','$Pname','$Pidno','$Psize','$Pcolour','$Pquantity','$Weblink','$Price','$tprice','$date','{$_SESSION['username']}') 
")or die(mysql_error()); 
} 
else 
echo 'price field requires numbers'; 
} 
else 
echo 'please fill in all required * fields '; 
} 
?> 
</div> 
<form action='youraccount.php' method='Post' class='slistbar'> 
    <!--<div> 
    <label for='shoppinglist' class='fixedwidth'></label> 
    <textarea type='text' name='shoppinglist' id='username' cols='100' rows='15'></textarea> 
    </div> --> 
    <div> 
    <label for='Sname' class='fixedwidth'> * Shop name</label> 
    <input type='text' name='Sname' id='Sname'/> 
    </div> 
    <div> 
    <label for='Pname' class='fixedwidth'> * Product name</label> 
    <input type='text' name='Pname' id='Pname'/> 
    </div> 
    <div> 
    <label for='Pidno' class='fixedwidth'> * Product id no /ad reference</label> 
    <input type='text' name='Pidno' id='Pidno'/> 
    </div> 
    <div> 
    <label for='Psize' class='fixedwidth'>Product size</label> 
    <input type='text' name='Psize' id='Psize'/> 
    </div> 
    <div> 
    <label for='Pcolour' class='fixedwidth'>Product colour</label> 
    <input type='text' name='Pcolour' id='Pcolour'/> 
    </div> 
    <div> 
    <label for='Pquantity' class='fixedwidth'>Product quantity</label> 
    <select name="Pquantity" id="Pquantity"> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="3">3</option> 
     <option value="4">4</option> 
     <option value="5">5</option> 
     <option value="6">6</option> 
     <option value="7">7</option> 
     <option value="8">8</option> 
     <option value="9">9</option> 
     <option value="10">10</option> 
</select> 
    </div> 
    <div> 
    <label for='Weblink' class='fixedwidth'> * Web link</label> 
    <input type='text' name='Weblink' id='Weblink'/> 
    </div> 
    <div> 
    <label for='Price' class='fixedwidth'> * Price GBP</label> 
    <input type='text' name='Price' id='Price'/> 
    </div> 
    <div> 

    <div class='buttonarea'> 
      <p> 
      <input type='submit' name='submit' value='Add'> 
      </p> 
      </div> 
      </p> 
    </form> 
</div> 
</div> 
<div class="action-box rounded"> 
<div class="titlebar"> 
<h2>Shopping List</h2> 
<a href='totalprice.php'>Update</a> 
</div> 
<div class="listbar"> 

    <form action='orderpplac.php' method='Post' class='slistbar'> 
    <table border='1'> 
    <tr> 
    <th>SHOP NAME</th> 
    <th>PRODUCT NAME</th> 
    <th>PRODUCT SIZE</th> 
    <th>PRODUCT COLOUR</th> 
    <th>PRODUCT QUANTITY</th> 
    <th>PRICE</th> 
    <th>TOTAL</th> 
    <th></th> 
    </tr> 
    <?php 
    // Get DB results and loop, outputting table rows with counter 
    $pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error()); 
    while ($row = mysql_fetch_assoc($pplresult)) { 
    echo " 
    <tr> 
    <td>".htmlspecialchars($row['Sname'])."</td> 
    <td>".htmlspecialchars($row['Pname'])."</td> 
    <td>".htmlspecialchars($row['Psize'])."</td> 
    <td>".htmlspecialchars($row['Pcolour'])."</td> 
    <td> 
    <input type='text' name='item[$i][Pquantity]' id='Pquantity' value='".htmlspecialchars($row['Pquantity'])."' /> 
    <input type='hidden' name='item[$i][Pidno]' id='Pidno' value='".htmlspecialchars($row['Pidno'])."' /> 
    </td> 
    <td>".htmlspecialchars($row['Price'])."</td> 
    <td> 
    <input type='text' name=tprice value='".htmlspecialchars($row['Tprice'])."' readonly> 
    </td> 
    <td><a href='deleteproduct.php?del=".htmlspecialchars($row['Pidno'])."'>delete</a></td> 
    </tr>"; 
    } 
    $pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error()); 
    while ($row = mysql_fetch_assoc($pplresult)) 
    //echo $row['Pquantity'] * $row['Price']; 
    { 
    $totalprice += $row['Tprice']; 
    } 
    //echo "$totalprice"; 

     ?> 
     <tr> 
       <th>Total Price</th> 
       <th><?php echo $totalprice; ?></th> 

      </tr> 
    <!--Close table and form--> 

    </table> 
    <input type='submit' name='submit1' value='UPDATE' /> 
    <input type='submit' name='submit2' value='SUBMIT' /> 
    </form> 
</div> 
</div> 
<div class="action-box rounded"> 
<div class="titlebar"> 
<h2> payment & balance</h2> 
</div> 
<div class="listbar"> 
</div> 
</div> 
</div> 
</div> 
</div> 
<?php include("include/footer.php");?> 

,這是更新

 <?php 
require_once("include/session.php"); 
require_once("include/dataconnect.php"); 
require_once("include/functions.php"); 
if (isset($_POST['submit1'])) { 
if(array_key_exists('item', $_POST)){ 

    //Loop through $_POST items, updating the database for each item 
    foreach ($_POST['item'] as $item) { 
     $Pquantity = intval($item['Pquantity']); 
     $Pidno = ($item['Pidno']); 
     $queryreg = mysql_query(" 
      UPDATE repplac 
       SET Pquantity = {$Pquantity} 
       WHERE 
         Pidno = '{$Pidno}' 
       AND 
         Uname = '{$_SESSION['username']}' 
     ") or die(mysql_error()); 

    } 
    } 
    $pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error()); 
    while ($row = mysql_fetch_assoc($pplresult)) 
    { 
    $Pidno = ($row['Pidno']); 
    $totalprice = $row['Price'] * $row['Pquantity']; 
    $queryreg = mysql_query(" 
    UPDATE repplac 
    SET Tprice = {$totalprice} 
    WHERE 
         Pidno = '{$Pidno}' 
       AND 
         Uname = '{$_SESSION['username']}' 
    ") or die(mysql_error()); 
    } 

    } 
    else if (isset($_POST['submit2'])) { 
    //Get Email Address 
    $emails = mysql_query("SELECT reusers.email FROM reusers INNER JOIN repplac ON reusers.username = repplac.Uname AND reusers.username = '{$_SESSION['username']}'")or die(mysql_error()); 
    //$emails = mysql_query("SELECT reusers.email FROM reusers INNER JOIN repplac ON reusers.username = repplac.Uname AND reusers.username = '".$_SESSION['username']."'")or die(mysql_error()); 
    $results = (mysql_fetch_assoc($emails)) or die(mysql_error()); 
    $email= $results['email']; 

    if(mysql_num_rows($emails) == 0){ 
     exit("No email addresses found for user '{$_SESSION['username']}'"); 
    } 
    $email = mysql_result($emails, 0); 

    //Get list to email user 
    $body = "<html><body><table border='1'> 
<tr> 
<th>Shop Name</th> 
<th>Product Name</th> 
<th>Size</th> 
<th>Color Name</th> 
<th>Quantity</th> 
</tr>"; 
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'"); 
while($row = mysql_fetch_assoc($pplresult)){ 
    $body .= "<tr> 
     <td>" . $row['Sname'] ."</td> 
     <td>" . $row['Pname'] ."</td> 
     <td>" . $row['Psize'] ."</td> 
     <td>" . $row['Pcolour'] ."</td> 
     <td>" . $row['Pquantity'] ."</td> 
     </tr>"; 
} 

$body .="</table></body></html>"; 

    //Send email 
$to = $email; 
    $subject = "YOUR ORDER LIST FROM REACHEASY"; 
$headers = "From: [email protected]\r\n"; 
$headers .= 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
mail($to,$subject,$body,$headers); 
     //Transfer records to wishlist 
    $transfer = mysql_query("INSERT INTO wishlist (SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}')")or die(mysql_error()); 
    // Delete temporary records if the above query was successful: 
    if($transfer !== false){ 
     $deletetable = mysql_query("DELETE FROM repplac WHERE Uname = '{$_SESSION['username']}'"); 
    } 
} 
redirect_to('youraccount.php'); 
?> 
+0

是一個錯字還是你的更新表單中有兩個表單標籤? – leon 2012-02-15 12:50:28

+0

@ leon其錯誤 – lostty84 2012-02-15 13:01:41

+2

請修復您的代碼然後 - 縮進和準確性 – leon 2012-02-15 13:08:57

回答

0

你的量被設置爲0,因爲INTVAL不能投你的量的有效數字。您需要調試$ item提交中返回的內容。

+0

當我有後期結果,它給這個數組([item] => Array([0] => Array([Pquantity] => 2)[1] => Array([Pidno] = > 123))[tprice] => 24 [submit2] =>提交),因爲這個字段我輸入的數量是2 – lostty84 2012-02-15 13:35:46

+0

您的輸出顯示$ item沒有'Pquantity'鍵。該值在一個子陣列中。看起來你需要$ item [0] ['Pquantity']或在你的循環$ item [$ i] ['Pquantity'] – davidethell 2012-02-15 21:47:20

+0

那是真的,我可以看到你在說什麼 – lostty84 2012-02-16 09:28:03

0

腳本你在哪裏得到上線

"<input type='text' name='item[$i][Pquantity]' id='Pquantity' value='".htmlspecialchars($row['Pquantity'])."' />" 

$i?您是否拋出所有錯誤,以便您可以看到PHP是否在抱怨什麼?你的錯誤級別是多少?

另外,你的代碼的最後一塊可以使用SUM SQL聚合函數,如:

SELECT SUM(Tprice) FROM repplac WHERE Uname = '{$_SESSION['username']}' 
+0

所有的錯誤,顯示 – lostty84 2012-02-15 13:19:24