I would very much appreciate if someone could take some moments to look over this code and tell me why this is apparently looping forever... whenever I run the script it times out...
I'm inserting products into a database, and I want to check whether:
1. It is already there (so don't re-enter the product
2. It is there, but in a different category (so append the category details with the new category)
3. It is there, and in the same category (so do nothing).
The first while loop (the database row retrieval) tells me that the product already exists. The second while loop tells me that the product is in the database, and is has already been entered for this category.
Why does this time out, if either of the two criteria are met? (if none are met, it's ok, and does nothing)
code:
// Check to see if the product by this reseller is already in the database
$query="SELECT id, class_id, full_url FROM products WHERE pid LIKE '$pid' AND manufacturer_id LIKE '$manufacturer_id'";
$ChkProduct_query=mysql_query($query);
while ($ChkProduct=mysql_fetch_row($ChkProduct_query)) {
$DBclassid=$ChkProduct[1];
//put db product class_id into an array, and check each value to see if an entry in this category already exists
$db_class_id_array=explode(", ", $ChkProduct[1]);
$db_class_id=reset($db_class_id_array);
//Check if product is already inserted into the category
while ($db_class_id) {
if ($db_class_id==$class_id) {
$chk_url=urldecode($ChkProduct[2]);
echo"<br><font color=\"red\"><b>This product has already been entered into this category.</b></font> ".
"<br>Click <a href=\"$chk_url\">HERE</a> to check that it is the same product.<br>";
$skip_insert=1; //do not insert the product
$skip_update=1; // do not update product, since it is already in this category
$db_class_id=next($db_class_id_array);
}
}
if (!$skip_update) { // the product in the database isn't in this category, so ok to update class_id
//append class_id to class_id in database
$new_class_id="$DBclassid, $class_id";
$query2="UPDATE products SET class_id='$new_class_id' WHERE id='$ChkProduct[0]'";
$update_classid=mysql_query($query2);
if (!$update_classid) {
echo"<br>There was a problem appending the class_id to the dbase class_id for product id $ChkProduct[0]";
}else{
echo"<br>The product was not inserted into the database, since it was already present. ".
"<br>The class_id for product id $ChkProduct[0] has been appended with the current class_id";
$skip_insert=1; // do not insert the product
}
}
}