I have 2 databases. 1 is a pricelist (allpet). 2 is an updated pricelist (import).
What I want to do is get all items from IMPORT and check each one against ALLPET. If a product code exists in ALLPET, update the price. If a product code does not exist INSERT a new record in ALLPET.
I have the following code but am stuck with regards the If then else statement and the correct coding for the SQL Queries $insert and $update.
<?php
$db = mysql_connect("localhost", "root", "jemq1312");
mysql_select_db("allpet",$db) or die ('Unable to connect to database');
$updated=0;
$inserted=0;
$q="SELECT * FROM import";
$update = "UPDATE allpet SET allpetcode= (SELECT import.allpetcode FROM import WHERE import.allpetcode=allpet.allpetcode) WHERE EXISTS (SELECT import.allpetcode FROM import WHERE import.allpetcode=allpet.allpetcode)"
$insert = "INSERT INTO allpet (allpetcode,description,costex) VALUES (import.allpetcode, import.description, import.costex)"
$result = mysql_query( $q, $db )or die(" - Failed query import :<br><pre>$q</pre><br>Error: " . mysql_error());
$num_rows = mysql_num_rows($result);
if ($myrow = mysql_fetch_array($result)) {
do {
// REQUIRES IF STATEMENT here to select either
mysql_query( $update, $db )or die(" - Failed UPDATE RECORD<br><pre>$q</pre><br>Error: " . mysql_error());
$updated++;
//ELSE
{ mysql_query( $insert, $db ) or die(" - Failed INSERT NEW RECORD<br><pre>$q</pre><br>Error: " . mysql_error());
$inserted++;
} while ($myrow = mysql_fetch_array($result));
}
mysql_free_result($result);
mysql_close($db);
echo "Upates = : " $updated;
echo "Insert = : " $inserted;
?>
Thanks for your help
Q