1

我正在尝试将数据插入数据库。没有错误,但数据库仍未更新。在数据库中,product表包含:productid、categoryid、productname、productimage、stock、price,detailsales表包含:transactionid、productid、数量

更新一个正在工作,但插入一个根本不起作用。

这是我的代码:

            <form action="doShop.php" method="post">
            <table border="1">
                <tr>
                    <td>Product ID</td>
                    <td>Product Name</td>
                    <td>Product Image</td>
                    <td>Price</td>
                    <td>Product Stock</td>
                    <td> Quantity</td>
                </tr>

                <?php
                    $query = mysql_query("SELECT * FROM Product");
                    while($row = mysql_fetch_array($query)){
                ?>
                <tr>
                    <td><input type="text" value="<?=$row['ProductID']?>" name="productid"></td>
                    <td><?=$row['ProductName']?></td>
                    <td><img src="image/<?=$row['ProductImage']?>"></td>
                    <td><?=$row['Price']?></td>
                    <td><input type="text" value="<?=$row['Stock']?>" name="stock"></td>
                    <td><input type="text" name="quantity"></td>
                </tr>

                <?php
                    }
                    print mysql_error();
                ?>
            </table>
            <table>
            <tr><input type="submit" value="Submit"></tr>
            </table>
            </form>

这是doShop代码:

                <?php
            include("connect.php");

            $id=$_REQUEST['productid'];
            $quantity=$_REQUEST['quantity'];
            $stock = $_REQUEST['stock'];

            mysql_query("insert into DetailSales(ProductID, Quantity)values('".$id."','".$quantity."')");
            mysql_query("update product set stock = $stock - $quantity where detailsales.productid = product.productid");
            header("location:shopcart.php");
            ?>

谁能帮我?

4

3 回答 3

3

你试图在你的查询中进行计算,而不是我创建了一个单独的变量$total来为你处理它。

像这样:

 $total = $stock - $quantity;

而不是这个:

SET stock = $stock - $quantity 

因此,将 doshop 代码更改为:

  <?php
      include("connect.php");
      $id = $_REQUEST['productid'];
      $quantity = $_REQUEST['quantity'];
      $stock = $_REQUEST['stock'];
      $total = $stock - $quantity;

      mysql_query("INSERT INTO DetailSales(ProductID, Quantity)
                   VALUES ('".$id."','".$quantity."')") or die(mysql_error());

      mysql_query("UPDATE product SET stock = '$total'
                   WHERE detailsales.productid = product.productid") 
                   or die(mysql_error());

     header("Location: shopcart.php");
于 2013-05-17T15:07:01.693 回答
0

试试这种方式;

$sql = ("insert into DetailSales(ProductID, Quantity)values('".$id."','".$quantity."')");
$res = mysql_query($sql);

if($res) {
    mysql_insert_id();
} else {
    die(mysql_error());
}
于 2013-05-17T15:10:32.977 回答
0
 mysql_query("update product set stock = $stock - $quantity where detailsales.productid = product.productid");  

我相信这条线是错误的,应该是

 mysql_query("update product set stock = ".($stock - $quantity)." where detailsales.productid = product.productid");  
于 2013-05-17T15:08:18.197 回答