diff --git a/classes/Attachment.php b/classes/Attachment.php index 0aa540c90..8becbcb30 100644 --- a/classes/Attachment.php +++ b/classes/Attachment.php @@ -127,12 +127,14 @@ class AttachmentCore extends ObjectModel public static function attachToProduct($id_product, $array) { $result1 = Attachment::deleteProductAttachments($id_product); + if (is_array($array)) { $ids = array(); foreach ($array as $id_attachment) if ((int)$id_attachment > 0) - $ids[] = '('.(int)$id_product.','.(int)$id_attachment.')'; + $ids[] = array('id_product' => (int)$id_product, 'id_attachment' => (int)$id_attachment); + Db::getInstance()->execute(' UPDATE '._DB_PREFIX_.'product SET cache_has_attachments = '.(count($ids) ? '1' : '0').' @@ -140,10 +142,10 @@ class AttachmentCore extends ObjectModel LIMIT 1 '); - return ($result1 && count($ids) && Db::getInstance()->execute(' - INSERT INTO '._DB_PREFIX_.'product_attachment (id_product, id_attachment) - VALUES '.implode(',', $ids)) - ); + if (!empty($ids)) + $result2 = Db::getInstance()->insert('product_attachment', $ids); + + return ($result1 && (!isset($result2) || $result2)); } return $result1; }