PHP问题更新数据库按钮单击

时间:2017-10-16 02:58:01

标签: php mysql database

您好我正在php中开店,我用while循环填充商店,这样就可以显示我数据库中的所有商品。这工作正常,但是当我按下购买按钮时,当我尝试更新帐户中剩余的库存数和金额时,我遇到了问题。

$ ItemCost变量只保存最后填充的项目成本,我不知道如何保存每个项目的成本以将其插入数据库。 $ StockCount变量还将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();
?>

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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>

}

?>