Jump to content

how Do i test if update was needed and perform action if update happened?


bidntrade

Recommended Posts

i have this code ....

 

it does not seem to work .....

I would like to test :

 

product_id  and  weight when updating to make sure they match

my variable .

 

and i would like to test to see if the price has changed .

 

 

$price = round($price, 2);
$query = 'SELECT cscart_products.product_id FROM cscart_products LEFT JOIN cscart_product_prices USING (product_id) WHERE product_id = ' . $id . ' AND weight = ' . $weight . ' AND price != ' . $price;
if ($result = mysql_query($query)) {
if($go){
// entry exists update
if(mysql_query("UPDATE cscart_product_prices PP LEFT JOIN cscart_products PI USING (product_id) SET PP.price = '$price', PI.amount = '$qty' WHERE PI.product_code = '$id' AND PI.weight = '$weight'")){
$emailinfo .= "Product ".$id." Price Updated To ".$price. " Stock= " . $qty . " .\n";
echo "Product ".$id." Price Updated To ".$price. " Stock= " . $qty . " .<br>";
}
}
}
$result = "";
$go='1';
}

 

go easy on me im very new to php ...

as you can tell from the code ...

 

the price variable sometime reads like this

 

9.5

 

but my database stores them like 9.50

 

i would rather not have to do the select and just test if the update

matched anything or not with a WHERE clause . but for some reason

even if its not there it returns true

 

Note:  the code im using this in is going through a csv file one line at a time nightly checking for price changes . there is about 3600 products in the csv.

 

HELP !

i would rather not have to do the select and just test if the update

matched anything or not with a WHERE clause . but for some reason

even if its not there it returns true

 

This is because mysql_query returns trueor false depending on wether or not the query succeeded or not, not if it found any results. There is a difference.

 

You need to check if any results where found in your select query. eg;

 

$sql = "SELECT * FROM foo WHERE id = 1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    // match found (do update).
  } else {
    // no match found (do insert).
  }
}

 

You should always check the number of rows found with select queries before attempting to use the result.

   

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.