Inserting returned array

Discussion in 'PHP' started by sensoryaddict, Sep 29, 2010.

  1. #1
    Hi I am working on a simple script.

    Basically I want to find matches in 2 tables and insert the match into a 3rd table.

    I am stuck on the 3rd part. I think that I am probably not handling the array properly, because I see am successfully getting back a result from the first query, but it fails to insert. I am not worried about the loop right now, just want to get it to work with the first row.


    $result = mysql_query("Select * From table1 Natural Join table2 Order By date ASC", $connection);
    if (!$result)
    {
    die("database query failed: " . mysql_error());
    }
    $row = mysql_fetch_array($result);

    print_r($row); // testing: I get Array ( [0] => 2010-09-24 [date] => 2010-09-24 [1] => AUBN [symbol] => AUBN )


    // now I try to insert results into table 3

    $date = mysql_real_escape_string($row['date']);

    $symbol = mysql_real_escape_string($row['symbol']);

    $query = "INSERT INTO table 3 (date, symbol) VALUES ($date, $symbol)";

    $insert = mysql_query($query, $connection);

    if (!$insert)

    {
    die("database query failed: " . mysql_error());
    }


    // I get error "database query failed: Unknown column 'AUBN' in 'field list'"
     
    sensoryaddict, Sep 29, 2010 IP
  2. sensoryaddict

    sensoryaddict Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    fixed

    added single quotes and curlies

    $query = "INSERT INTO table 3 (date, symbol) VALUES ('{$date}', '{$symbol}')";
     
    sensoryaddict, Sep 29, 2010 IP