Jump to content

help with arrays and inserting. thanks in advance!!!


jcrensha627

Recommended Posts

hello everyone - i am creating a phpsql stock market simulation game. ;D

 

I have no obvious error but now something is obviously wrong with what is inserted- here are my last 3 purchases, the table from phpmyadmin is below. what i am trying to do is volume, which is the simply the sum of all values in the shares column. as of now i am just gettin a "0" when i purchase stock. here is what should be seen in the table results looking from phpmyadmin.

 

 

id 105's volume should be 10 106's should be 20 and 107's should be 30 so on and so forth. to have a running total volume for this stock - in the end i would also like the price of the stock to change, based amount of buys or volume. im assuming price change would be this same theory.

 

id symbol shares volume price increase

107 JAME 10 0 jimbo 500

106 JAME 10 0 jimbo 500

105 JAME 10 0 jimbo 500

53 JAME 0 50.00 0

 

i believe this is happening because of $volume, but i was assuming it would take its end result

using this -----echo 'volume = '.$volume = $volume + $row['shares'] ; although could my problem also be how i am inserting into JAME_PH?? right now im trying to insert $volume. i think this is where im going wrong, did i leave anything in or out? thanks in advance ???

 

 

 

here is my code------------

 

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


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


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

$get_my_basket = mysql_query("SELECT `symbol`, `price` FROM `JAME_PH` WHERE `symbol`='".JAME."'");

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


// my attempt at volume is below, from a friend on net.

$query = "select * from JAME_PH WHERE `symbol`='".JAME."'";
   $result = mysql_query($query); 
   while ( $row = mysql_fetch_array($result) ) {
echo $row['volume']; // echo them out
}



$volume = 0;

$result = mysql_fetch_assoc($result);  
{
    echo 'id '.$row['id'];
    echo 'symbol '.$row['symbol'];
    echo 'shares '.$row['shares'];
    // add to the volume and print
    echo 'volume = '.$volume = $volume + $row['shares'] ;
    echo 'price = '.$row['price'];
}  

$insert = mysql_query("insert into trades values ('NULL', 'JAME', '".$_POST['quantity']."', '$total_cost', '$username' )")
or die("Could not insert data because ".mysql_error());

//where i insert volume into table JAME_PH
$insert2 = mysql_query("insert into JAME_PH values ('NULL', 'JAME', '".$_POST['quantity']."', $volume, '$username','$total_cost', '$username' )")
or die("Could not insert data because ".mysql_error());

ob_end_flush();
?>


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

</body>
</html>

//where i insert volume into table JAME_PH

$insert2 = mysql_query("insert into JAME_PH values ('NULL', 'JAME', '".$_POST['quantity']."', $volume, '$username','$total_cost', '$username' )")

or die("Could not insert data because ".mysql_error());

 

You forgot to add '' around $volume :)

 

I'm kind of a novice myself so I'm not sure why you are submitting your quantity value in that way. Make sure you don't forget to filter the $_POST data before submitting it to your table! If someone submit something hairy in place of quantity you could have allot of problems.

I would try doing the mysql statement as:

 

$insert2= "INSERT INTO JAME_PH

(ID, symbol, shares, volume, username, price, increase, username2)

VALUES('', 'JAME', '$quantity', '$volume', '$username', '$total_cost', '$username' ')";

 

mysql_query($insert2) or die(mysql_error());

 

I got kind of confused looking at your INSERT statement because there were two user names (?) and the format didn't match the phpmyadmin table you started out with.

 

Try explicitly naming your columns so that you are sure the order is correct and there is no extra variable getting in the way of things.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.