Forum Moderators: coopster

Message Too Old, No Replies

need help with inserting or updating info into DB

sorry if this is the wrong forum

         

jcrensha627

4:56 am on Feb 28, 2009 (gmt 0)

10+ Year Member



Below I have some php code. It is going to be a sort of stock market simulation. I have created a stock JAME in a table in my db along with the price of it as well as an auto inc id column. Below is what i have so far, I take the stock SYMBOL (JAME) and multiply by the quantity that is provided by the logged in user. this gives me a total purchase cost based on the set price in the table and the quantity posted. what i want is to insert this info into a table in the DB

who purchased it
what they purchased (shares,price,symbol)

<html>
<body>
Go Back to<a href="index.php">Your Portfolio</a>
<br>
<br>
Are you sure you want to buy shares in JAME? (Total Below)
<br>
<br>

<?php
ob_start();
include("config.php");

$username = $_COOKIE['loggedin'];
if (!isset($_COOKIE['loggedin'])) die("You are not logged in, <a href=../login.html>click here</a> to login.");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

$get_my_basket = mysql_query("SELECT `symbol`, `price`, `customer` FROM `Athletes` WHERE `customer`='".$username."'");

$total_cost = 0;
$JAME = 0;
$quantity = $_POST['quantity'];

while($my_basket = mysql_fetch_array($get_my_basket))
{
if($my_basket['symbol']=='JAME') $JAME++;

$total_cost += $my_basket['price'] * $quantity;
}

echo 'You have selected: ';
if($JAME > 0)
{
echo $quantity.' JAME shares, ';
}
else
{
echo 'Your Basket it Empty';
}
$total_cost = number_format($total_cost,2);

echo ' and it will cost you $'.$total_cost;

ob_end_flush();
?>

<br>
<br>
Visit<a href="index.php">Your Portfolio</a> to see your purchases.

</body>
</html>

coopster

12:49 am on Mar 3, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, jcrensha627.

First step is going to be to develop the necessary INSERT statement that will populate the table you have created to store the data. Don't forget to scrub the user data, make sure it is what you expect. Then, always use mysql_real_escape_string [php.net] to condition the user-supplied information before attempting to use it in an sql query statement.