Hi, I am a newbie to php and sql.
I have a table called allpet_twb this contains products and pricing.
I have another table called import_allpet that contains products and updated pricing as well as new products.
What I am trying to do is go through each record in import_allpet and see if it is in allpet_twb (which is currently empty so my code shoudl insert new records). If so update the costex field otherwise insert a new record and assign the data from import_allpet to allpet_twb.
Here is my code.
// assign the query
$query = "SELECT * FROM import_allpet";
// Execute the query
// Execute the query
$result = mysql_query( $query );
if (!$result){
die ("Could not query the database: <br />". mysql_error( ));
}
// Setup the Table
echo "<table width='0' border='1' cellpadding='1'>";
echo "<tr>";
echo "<th scope='col'>TWB Code</th>";
echo "<th scope='col'>Allpet Code</th>";
echo "<th scope='col'>Description</th>";
echo "<th scope='col'>Cost Ex $</th>";
echo "<th scope='col'>Updated</th>";
echo "</tr>";
// Fetch and display the results
while ($result_row = mysql_fetch_row(($result))){
$query2 = "SELECT * FROM allpet_twb WHERE allpetcode=$result_row[1]";
$result2= mysql_query( $query2 );
if (!$result2) {
mysql_query( "INSERT INTO allpet_twb (allpetcode,description,costex) VALUES
$result_row[1],$result_row[2],$result_row[3] " );
echo"<tr>";
echo"<td> $result_row[4]</td>";
echo"<td> $result_row[1]</td>";
echo"<td> $result_row[2]</td>";
echo"<td align='right'>$ $result_row[3]</td>";
echo"<td> 'NEW'</td>";
echo"</tr>";
}
if ($result2){
mysql_query( "UPDATE allpet_twb SET costex=$result_row[3] WHERE allpetcode=$result_row[1]");
echo"<tr>";
echo"<td> $result_row[4]</td>";
echo"<td> $result_row[1]</td>";
echo"<td> $result_row[2]</td>";
echo"<td align='right'>$ $result_row[3]</td>";
echo"<td> 'UPDATED'</td>";
echo"</tr>";
}
}
When I run the code it always shows "UPDATED" as per the if ($result2) test but the table is not updated.
I also want to add some extra values into the allpet_twb table to show if the price has increased, decreased, the product is NEW or the product has been discontinued (i.i it is in the old pricelist but not the new one.
Thanks in adavnce
Jemq