Jump to content

Post to database partial with form


mitzter

Recommended Posts

Hello,

 

I wrote a script where i can enter a stock symbol and it get saved in my database. This works, but when i enter the stock symbol i would like to also get the current stock price get saved into the database.

 

The link for the stock price (i.e. AAPL): http://quote.yahoo.com/d/quotes.csv?s=AAPL&f=l1&e=.csv

 

How do i programm this in the script below?

 

Besides above, notice that "AAPL" in the link above has to be a varaible, which changes where i for example type in "GS".

 

Form:

<html>

<body>

<form action="portfolio/insert_buy.php" method="post">

Symbol: <input type="text" name="symbol"><br />

<input type="submit" value=" Submit">

</form>

</body>

</html>

 

PHP-script

<?php

$conn = mysql_connect("xxx","xxx","xxx");

$db      = mysql_select_db("xxx",$conn);

 

$sql="INSERT INTO portfolio (symbol)

VALUES

('$_POST[symbol]')";

 

if (!mysql_query($sql,$conn))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";

 

mysql_close($conn);

?>

 

 

I hope someone can help me out, thanks!

Link to comment
Share on other sites

Okay i somewhat understand what you are asking. If im not mistaken, you have a ticker symbol and you want to use that to get the current price of the symbol. The link you provided is a .csv file. PHP has a function called fgetcsv() which you can use to parse the information. The returned value is an array, usually parsed by field.

 

<?php
   $conn = mysql_connect("xxx","xxx","xxx");
   $db      = mysql_select_db("xxx",$conn);

   $getPrice = file('http://quote.yahoo.com/d/quotes.csv?s=AAPL&f=l1&e=.csv'); //opens the file and gets the contents and puts it in an array
   
// the $getPrice array now contains your price, you will place it where ever you need by using $getPrice[0]

$sql="INSERT INTO portfolio (symbol)
VALUES
('$_POST[symbol]')";

if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($conn);
?>

 

 

hope this helped your question

 

algid

Link to comment
Share on other sites

Thanks for your answer. In place of getCSV i made it with cURL and some adjustments.

 

In short what i want to have the code done:

I want that someone can put a ticker symbol in the form (see 1st post) and then the ticker + last price of that ticker get posted into my database. With the form the user can post the ticker, but the last price would not go thru the form but automatic.

 

So input = ticker and output = ticker + last price

 

 

I now have the following code, but it doesn't work. The basis is correct, but i've done somethind wrong. Can someone please help me with the script to get it work?

 

<?php

if(isset($_POST['symbol'])){
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s='.$_POST['symbol'].'&f=sl1d1t1c1ohgv&e=.csv' );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );

$contents = explode( ',', str_replace( '"', '', $output ) );
    echo "<p>AAPL stock: <b>\$$contents[1]</b> ( $contents[4] )</p>";
    $conn = mysql_connect("xxx","xxx","xxx");
    $db      = mysql_select_db("xxx",$conn);

$sql="INSERT INTO portfolio (symbol)
VALUES
('$_POST[\'symbol\']')";

if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($conn);
} else {
echo '
<form action="portfolio/insert_buy.php" method="post">
Symbol: <input type="text" name="symbol"><br />
<input type="submit" value=" Submit">
</form>';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.