2017-10-16 36 views
2

你好我正在一家商店用PHP來填充商店的一個while循環,以便顯示我數據庫中的所有商店商品。這工作正常,但我有一個問題,當我嘗試更新該帳戶上剩餘的股票數量和錢時,我按購買按鈕。PHP問題在按鈕上更新數據庫單擊

$ ItemCost變量只保存上次填充的項目成本,我不確定如何保存每個項目的成本以將其插入到數據庫中。 另外$ StockCount變量將庫存計數設置爲1.

我該如何解決這個問題。

<?php 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "useraccounts"; 

// Create connection 
$conn = mysqli_connect($servername, $username, $password, $dbname); 

$GatherItems = "SELECT * FROM shopitems WHERE StockCount > 0 ORDER BY`shopitems`.`Cost` DESC"; 
$result = $conn->query($GatherItems); 

if ($result->num_rows > 0) { 
// output data of each row 

while($row = $result->fetch_assoc()) { 
    $ItemName = $row['ItemName']; 
    $ItemCost = $row['Cost']; 
    $ID = $row['ID']; 
    $StockCount = $row['StockCount']; 
    $Money = $row['Money']; 

echo "<div class='test'>$ItemName</div>"; 
echo "<div class='test1'>$ItemCost </div>"; 
echo "<input type='submit' class='btn btn-primary' name='Buy' value='Buy Now'/>"; 
} 

$NewTotal = $Money - $ItemCost; 
$Inventory = "UPDATE shopitems SET StockCount = $StockCount-1, Money = $NewTotal WHERE ID = $ID"; 

if(isset($_POST['Buy'])){ 
if ($conn->query($Inventory) === TRUE) { 
    echo "New record created successfully"; 
} else { 
echo "Error: " . $Inventory . "<br>" . $conn->error; 
    } 
} 
} 
$conn->close(); 
?> 
+1

你需要通過與按鈕點擊該行的'ID',並檢查該'ID'。此外,您還需要在您的'SELECT'腳本之前移動'UPDATE'腳本,因爲您在選擇後刪除了腳本,因此您的總計將始終關閉。 – Sean

回答

0

正如@Sean說,你能做到這一點,如:

<?php 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "useraccounts"; 

// Create connection 
$conn = mysqli_connect($servername, $username, $password, $dbname); 

if(isset($_POST['Buy'])){ 
// update stock and money 

$ID = $_POST['ID']; 
$Money = $_POST['Money']; 
$ItemCost = $_POST['ItemCost']; 
$NewTotal = $Money - $ItemCost; 
$Inventory = "UPDATE shopitems SET StockCount = $StockCount-1, Money = 
$NewTotal WHERE ID = $ID"; 

if ($conn->query($Inventory) === TRUE) { 
    echo "New record created successfully"; 
} else { 
echo "Error: " . $Inventory . "<br>" . $conn->error; 
    } 
} 

// display items 

$GatherItems = "SELECT * FROM shopitems WHERE StockCount > 0 ORDER 
BY`shopitems`.`Cost` DESC"; 
$result = $conn->query($GatherItems); 

if ($result->num_rows > 0) { 
// output data of each row 

while($row = $result->fetch_assoc()) { 
    $ItemName = $row['ItemName']; 
    $ItemCost = $row['Cost']; 
    $ID = $row['ID']; 
    $StockCount = $row['StockCount']; 
    $Money = $row['Money']; 
    echo "<form method='post' action=''>"; 
    echo "<div class='test'>$ItemName</div>"; 
    echo "<div class='test1'>$ItemCost </div>"; 
    echo "<input type='hidden' name='Id' value='".$ID."'/>"; 
    echo "<input type='hidden' name='Money' value='".$Money."'/>"; 
    echo "<input type='hidden' name='ItemCost' value='".$ItemCost."'/>"; 
    echo "<input type='submit' class='btn btn-primary' name='Buy' value='Buy Now'/>"; 
    echo "</form>"; 

} 
} 

$conn->close(); 
0

假設你不想Ajax和你不想JS,但只有在每個「買入」按鈕項目將重新打開整個頁面,然後你想要這樣的東西。這是代碼:

//FIRST we need to process the form: 
<?php 
if(isset($_POST['submit'])){ 
    $itemId = $_POST['id']; 
    //do the stuff. Remember about escaping $itemId, or using prepared statements 
    //select... from where id = $itemId 
} 
?> 

//now get the items: 
<?php 
    $GatherItems = ... 
?> 

//now the html: 

<?php 
while($row = $result->fetch_assoc()) { 

?> 
<form method = 'post'> 
    <div class='test'><?=$ItemName?></div> 
    ... 
    <input type = 'hidden' name = 'itemId' value = '<?=$ID?>'> 
    <input type = 'submit' name = 'submit' value = 'Buy'> 
</form> 

} 

?>