1

I'm trying to insert data to database. There is no error, but the database is still not being updated. In the database, the product table contains: productid, categoryid, productname, productimage, stock, price and the detailsales table contains: transactionid, productid, and quantity

The update one is working, but the insert one is not working at all.

Here is my code:

            <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>

Here is the doShop code:

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

Can anyone help me?

7
  • How do you know there is no error? You're not checking mysql_error(). Commented May 17, 2013 at 15:03
  • 6
    mysql_query has been deprecated. Commented May 17, 2013 at 15:05
  • I see this type of question all the time, you should read common database debugging for PHP and MySQL. Commented May 17, 2013 at 15:06
  • Lovely SQL injection attack vulnerabilities. Enjoy having your server pwn3d. Commented May 17, 2013 at 15:08
  • if you want to be sure a query is working go to your mysqld.log, fetch the exact query mysql is receiving and paste it on your phpmyadmin query console. Commented May 17, 2013 at 15:08

3 Answers 3

3

You were trying to carry out calculations inside your query, instead I created a separate variable called $total to handle that for, you.

Like this:

 $total = $stock - $quantity;

Instead of this:

SET stock = $stock - $quantity 

So, change the doshop code to this:

  <?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");
Sign up to request clarification or add additional context in comments.

1 Comment

exactly what I was about to write
0
 mysql_query("update product set stock = $stock - $quantity where detailsales.productid = product.productid");  

I believe this line is wrong and should be

 mysql_query("update product set stock = ".($stock - $quantity)." where detailsales.productid = product.productid");  

1 Comment

sql can do math. update foo set field=10-4 will set field to 6.
0

Try this way instead;

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.