Update Database

Posted on 16th Feb 2014 by admin

Hi All,


I have a problem with this:

Code: [Select]<?php
session_start();
include('../common/dbconnect.php');
$stock_id = $_GET['stock_id'];



$query='SELECT * FROM users_stocks WHERE user_id ="' . $_SESSION['user_id'] . '"';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
$row = mysql_fetch_array($result);
extract($row);
$query1="SELECT t_val FROM stocks WHERE stock_id = '$stock_id'";
$result1 = mysql_query($query1, $conn) or die(mysql_error($conn));
$rows = mysql_fetch_array($result1);
extract($rows);
$query2="SELECT quant FROM users_stocks_details WHERE stock_id = '$stock_id' AND user_id ='" . $_SESSION['user_id'] . "'";
$result2 = mysql_query($query2, $conn) or die(mysql_error($conn));
$rows2 = mysql_fetch_array($result2);
extract($rows2);

//the values
$t = $row['total'];
$b = $row['bank'];
$t_val=$rows['t_val'];
$q = $_GET['quantity'];
$quant =$rows2['quant'];


//sums
$newb = $q * $t_val + $b;
$newt = $t + $newb;
$newquant = $quant - $q;

//the interesting bit

if ($q == 0)
{
echo "You Dont Have Any Stocks To Sell!";

}

if ($q > 0)

{
$updatebank = "UPDATE users_stocks SET bank = $newb WHERE user_id ='" . $_SESSION['user_id'] . "'";
$updatetotal = "UPDATE users_stocks SET total = $newt WHERE user_id ='" . $_SESSION['user_id'] . "'";
$updatequant = "UPDATE users_stocks_details SET quant = $newquant WHERE stock_id = '$stock_id' AND user_id ='" .$_SESSION['user_id']."'";


$showresults = "SELECT * FROM users_stocks WHERE user_id ='" . $_SESSION['user_id'] . "'";
$results = mysql_query($showresults, $conn) or die(mysql_error($conn));
$rrows = mysql_fetch_array($results);
extract($rrows);

echo "Thank you! You have succesfully sold $q stocks and you bank balance has incresed to $$newb, you have $newquant of these stocks left

";

}
?>
The total and bank updates work as they should, however the quantity of stocks does not decrease! Anyone see anything obvious?

Thanks

Other forums