0

Data entered by user:

"product_description": [{
    "language_id": 1,
    "name": "okayt321sd1a"
}

Update database:

foreach ($data['product_description'] as $product_description) {
    foreach ($product_description as $key => $value) {
        $a[$key] = $value;

        if($a[$key]){
            $this->db->query("UPDATE " . DB_PREFIX . "product_description SET language_id = '" . (int)$language_id . "', name = '" . $this->db->escape(html_entity_decode($product_description['name'])) . "', meta_keyword = '" . $this->db->escape(html_entity_decode($product_description['meta_keyword'])) . "', meta_description = '" . $this->db->escape(html_entity_decode($product_description['meta_description'])) . "', description = '" . $this->db->escape($product_description['description']) . "', tag = '" . $this->db->escape($product_description['tag']) . "',
                                  page_title = '" . $this->db->escape($product_description['page_title']) . "',
                                  highlight = '" . $this->db->escape($product_description['highlight']) . "',
                                  whatbox = '" . $this->db->escape($product_description['whatbox']) . "' WHERE product_id = '" . (int)$product_id . "', 
                              ");
        }
    }
}

How can I update data based on user enter, for example, if user enter

"language_id": 1,
"name": "okayt321sd1a"

then I only update this 2 data to database other data will remain, if enter 3 data:

"language_id": 1,
"name": "okayt321sd1a"
page_title

then I only update this 3 data to database other data will remain.

1 Answer 1

1

You probably want to do a function that will build the sql for you. I am assuming you are using PDO (I don't know, but this example uses PDO). You should bind parameters/values either way. To add in custom fields, just use array_merge(). You can use array_diff() or unset() to remove unwanted values/keys:

function updateProductDesc($array,$where,$db)
    {
        foreach($array as $key => $value) {
            $sKey           =   ":{$key}";
            $bind[$sKey]    =   htmlspecialchars($value);
            $sql[]          =   "`{$key}` = {$sKey}";
        }

        $bind[":where"] =   (int) $where;
        $query  =   $db->prepare("UPDATE ".DB_PREFIX."product_description SET ".implode(", ",$sql)." WHERE `product_id` = :where");
        $query->execute($bind);
    }

foreach ($data['product_description'] as $product_description) {
    foreach ($product_description as $key => $value) {
        $a[$key] = $value;
        if($a[$key]){
            updateProductDesc($product_description,$product_id,$this->db);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.