Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
help with inserting or updating??
Old 02-28-2009, 12:32 AM help with inserting or updating??
Novice Talker

Posts: 14
Name: biklad
Trades: 0
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>
jcrensha627 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-28-2009, 05:34 AM Re: help with inserting or updating??
EdB
Skilled Talker

Posts: 79
Name: Ed Barnett
Trades: 0
You want to check whether to update or insert - is that right? I.e. if the user has already bought shares in JAME then you don't want to duplicate the row but 'INSERT'ing instead of 'UPDATE'ing. Best thing to do is search the relevant table using SELECT for a row that contains that specific user's username and the JAME stock.

Code:
$sql = "SELECT id FROM `table` WHERE username='$username' AND stock='JAME'";
$query = @mysql_query($sql);
 
if (mysql_num_rows($query) < 1) { // If the row doesn't exist already
 
 // Use your insert code here
}
else { // The row exists already therefore you want to update it
 
 // User your update code here
}
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
Old 02-28-2009, 11:49 AM Re: help with inserting or updating??
Novice Talker

Posts: 14
Name: biklad
Trades: 0
Quote:
Originally Posted by EdB View Post
You want to check whether to update or insert - is that right? I.e. if the user has already bought shares in JAME then you don't want to duplicate the row but 'INSERT'ing instead of 'UPDATE'ing. Best thing to do is search the relevant table using SELECT for a row that contains that specific user's username and the JAME stock.

Code:
$sql = "SELECT id FROM `table` WHERE username='$username' AND stock='JAME'";
$query = @mysql_query($sql);
 
if (mysql_num_rows($query) < 1) { // If the row doesn't exist already
 
 // Use your insert code here
}
else { // The row exists already therefore you want to update it
 
 // User your update code here
}
thanks for the reply - this look close i think, but im trying to have it so that everytime a user makes a purchase it add another row with the corresponding stock symbol, username, price and total value
jcrensha627 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to help with inserting or updating??
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12106 seconds with 12 queries