0

I have an array I wish to loop through and apply some SQL if the record exists. I have something Like this:

$discontinuedProducts = ["MTUC7000AWS","MTUC7000AWB"];
$updateDiscontinuedProductsToInactive = mysql_query("UPDATE 'table_name' SET 'available_for_order' = 0 WHERE 'product_id' = $discontinuedProducts  ");

but I think my code isn't even executing, they're just being declared.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126

1 Answers1

0

Your sql statement can not possibly work because the variable $discontinuedProducts that you are concatenating is not a string but an array. You will need to modify your code like this

// make string from the array
foreach($discontinuedProducts as $discontinuedProduct)
{
      // put single quotes around productname, comma at end and add to string
      $whereinstring .= "'$discontinuedProduct',";
}

// remove trailing comma
$whereinstring = trim($whereinstring,',');

// the sql
$sql = "UPDATE 'table_name' 
        SET 'available_for_order' = 0 
        WHERE 'product_id' IN ( $whereinstring ) ";

// and use this sql string in the mysqlfunction of your choice ....
Freud Chicken
  • 525
  • 3
  • 8