Frunky
Frunky

Reputation: 385

How to add value to row by id sql

I have two arrays with ID and description.

In database I have same ID but doesn't have description.

How I can add each description form array to current ID?

This is full code

foreach($product->find('.block-d .btns-d .btn-buy') as $productId) {
    if(!empty($productId)) {
        dataId = $productId->{'data-offerid'};
    }
}

foreach($product->find('.description div div p') as $description) {
    if(!empty($description)) {
        $query = "UPDATE snowcore_parser_products SET description = " . $description . " WHERE remote_id = " . $dataId . " ';";
        $sql = mysqli_query($db, $query);
    }
}

If I try to use just simple value without array it works. For example

$query = "UPDATE snowcore_parser_products SET description = '1';";

Upvotes: 0

Views: 72

Answers (2)

Asperitas
Asperitas

Reputation: 329

I think your query is malformed. It doesn't have quotes around the description to indicate it is a string. For example, if the value for description is "stackoverflow" and the id is "1", your query would look like so:

UPDATE snowcore_parser_products SET description = stackoverflow WHERE remote_id = 1 ';

So to fix this, the last quote should disappear and the value for description should be surrounded with quotes. Like this:

$query = "UPDATE snowcore_parser_products SET description = '" . $description . "' WHERE remote_id = " . $dataId . ";

Also I recommend you to read this article on SQL injection, as this query isn't safe.

Upvotes: 1

TheMY3
TheMY3

Reputation: 353

just use foreach and that`s all, try this:

$ids = [1,2,3];
$descriptions = [1,2,3];
foreach($ids as $key => $id) {
    $query = "UPDATE snowcore_parser_products SET description = " . $descriptions[$key] . " WHERE remote_id = " . $id . " ';";
    $sql = mysqli_query($db, $query);
}

Upvotes: 1

Related Questions