Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    insert mysql select query with php, empty variable?

    Here's the query I'm trying to insert into a table in MYSQL

    Code:
    $query1 = "SELECT close FROM stocks WHERE the_date = '$the_date' AND ticker = '$ticker' ";
    $result1 = mysql_query($query1) or die("Query failed : " . mysql_error());
    I managed to print it out like this:

    Code:
    while($price = mysql_fetch_assoc($result1)){
      echo "Price: ".$price['close']."<br /></h2>";
         }
    And I tried inserting with this:

    Code:
    $query3 = "INSERT INTO transactions (trans_ID,the_date,client_ID,ticker,shares,price,buy)
               VALUES (\"$trans_ID\",\"$the_date\",client_ID,\"$ticker\", 
                $shares,$price,$buy)";
    But the error message on the page said this: "Query failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1)' at line 3"

    which is weird cause that's just where my html/head tags are.

    So the I tried to do this:
    var_dump($price); and got bool(false)

    so how do i make it so $price actually has something in it and will allow me to insert into the mysql table?

    thanks!

  • #2
    Regular Coder
    Join Date
    Jun 2009
    Posts
    138
    Thanks
    3
    Thanked 19 Times in 19 Posts
    Dont escape the "" marks in your INSERT query

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,010
    Thanks
    2
    Thanked 312 Times in 304 Posts
    which is weird cause that's just where my html/head tags are.
    The sql error message is referring to line 3 of the sql statement, not line 3 of your .php code file.

    If $price['close'] is the price, you would need to store that into a php variable to use in your INSERT query. You are using $price, which is actually a false value after the end of your while(){} loop.
    Last edited by CFMaBiSmAd; 05-06-2013 at 03:20 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •