-4

I have two tables - oldtbldata & tbldata, and want to read a row in the first and insert it into the second table. One new column, txtNotes does not exist in the old table, I want to set that to a space.

The oldtbldata has rows that do not exist in tbldata, and I want to insert these into the tbldata as new rows.

I have established an original connection (conn) to do the initial search for matching txtWoodIDcode entries, if a row is found that does not exist, I want to add that to the tbldata using conn2.

The initial code is:

    $conn = mysql_connect('localhost', '***', '*****');
if(! $conn )
  {
  die('Could not connect: ' . mysql_error());
  }

echo "Conn connected<br>";


$sql = 'SELECT oldtxtWoodIDcode FROM oldtbldata';
mysql_select_db('scw-db');

If the search does not find the ID, it goes to this code:

    {
    $conn2 = mysql_connect('localhost', '***', '***');
    if(! $conn2 )
      {
      die('Could not connect  #2: ' . mysql_error());
      }

    echo "Conn2 connected<br>";
    $sql_new = 'SELECT txtWoodIDcode FROM tbldata';
    mysql_select_db('scw-db');
    $retval = mysql_query( $sql, $conn2 );
    if(! $retval )
    {
      die('Could not get data from conn2: ' . mysql_error());
    }

    $sql_new = "INSERT INTO tbldata 
    (
        'txtWoodIDcode',
        'txtProductCode',
        'txtNotes',
        'txtSpecies',
    )
    VALUES 
    {
        'oldtxtWoodIDcode',
        'oldtxtProductCode',
        ' ',
        'oldtxtSpecies',
    )";

    echo "past the insert code<br>";

    if ($conn2->query($sql_new) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql_new . "<br>" . $conn2->error;
    }               
    mysql_close($conn2);
}

The error I get is on the if ($conn2->query($sql_new) === TRUE) line:

Fatal error: Call to a member function query() on a non-object

And yes, I get the "Conn2 connected" message, so I should be connected to the DB.

I am new to PHP/MySQL, and working with an old version - so I know I need to upgrade - but will need to do that later. I just need to get THIS code working now.

Stephen
  • 135
  • 2
  • 15

1 Answers1

0

you have few mistakes - you don't have in mysql ... $conn2->query ... use mysql_query function - in the query you using single quot for the fields name ' ... you need to use ` , and remove the comma , after the kast field

$conn2 = mysql_connect('localhost', '***', '***');
if(! $conn2 )
  {
  die('Could not connect  #2: ' . mysql_error());
  }

echo "Conn2 connected<br>";
$sql_new = 'SELECT txtWoodIDcode FROM tbldata';
mysql_select_db('scw-db');
$retval = mysql_query( $sql, $conn2 );
if(! $retval )
{
  die('Could not get data from conn2: ' . mysql_error());
}

$sql_new = "INSERT INTO tbldata 
(
    `txtWoodIDcode`,
    `txtProductCode`,
    `txtNotes`,
    `txtSpecies`
)
VALUES 
{
    'oldtxtWoodIDcode',
    'oldtxtProductCode',
    ' ',
    'oldtxtSpecies'
)";

echo "past the insert code<br>";

if ($conn2->query($sql_new) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql_new . "<br>" . $conn2->error;
}               
mysql_close($conn2);
shushu304
  • 1,403
  • 1
  • 5
  • 15