mitzter Posted May 16, 2012 Share Posted May 16, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/262636-post-to-database-partial-with-form/ Share on other sites More sharing options...
algidDes702 Posted May 16, 2012 Share Posted May 16, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262636-post-to-database-partial-with-form/#findComment-1346116 Share on other sites More sharing options...
mitzter Posted May 18, 2012 Author Share Posted May 18, 2012 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262636-post-to-database-partial-with-form/#findComment-1346571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.