2013-05-17 133 views
1

我试图插入数据到数据库。 没有错误,但数据库仍未更新。 在数据库中,product表包含: 产品ID,类别ID,产品名称,productimage,股票价格 和detailsales表包含: 的transactionId,产品ID和数量无法插入到mysql数据库

更新一个工作,但插入的一个根本不工作。

这里是我的代码:

  <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"); 
      ?> 

谁能帮助我?

+0

你怎么知道没有错误?你不检查'mysql_error()'。 – andrewsi

+6

['mysql_query'](http://php.net/manual/en/function.mysql-query.php)已被弃用。 – Blazemonger

+0

我总是看到这种类型的问题,你应该阅读[PHP和MySQL的常见数据库调试](http://jason.pureconcepts.net/2013/04/common-debugging-php-mysql/)。 –

回答

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"); 
+1

正是我正要写 – Naryl

+0

会更好,如果您还提供您解决了这个问题。 – Bart

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"); 
+2

sql可以做数学。 'update foo set field = 10-4'会将'field'设置为'6'。 –

+0

嗯,你是:P – beiller

0

尝试,而不是这样;

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

if($res) { 
    mysql_insert_id(); 
} else { 
    die(mysql_error()); 
}