0

如果参数存在,我正在尝试更新此代码,但是,当我执行任何操作时(它确实说已完成,并更新产品,但 products_description 的更新似乎不起作用。代码在这里:

    $query =   "UPDATE products 
            SET products_reference='$varenummer', products_price='$pris'
            WHERE products_id='$id';";

            for ($x = 1; $x <= $amountOfLanguages; $x++) {
                $varenavn = $_POST["varenavn-" . $x];
                $korttekst = $_POST["korttekst-" . $x];
                $langtekst = $_POST["langtekst-" . $x];

                $query .=  "IF EXISTS (SELECT * FROM products_description WHERE products_id='$id' AND languages_id='$x')
                                UPDATE products_description
                                SET products_description_name='$varenavn', products_description_short_description='$korttekst', products_description_description='$langtekst'
                                WHERE products_id='$id'
                                AND languages_id='$x'
                            ELSE
                                INSERT INTO products_description (products_id, languages_id, products_description_name, products_description_short_description, products_description_description)
                                VALUES ('$id', '$x', '$varenavn', '$korttekst', '$langtekst');";
            } 

            if ($conn->multi_query($query) === TRUE) {
                //header("Location: ./");
                //die();
                echo "done";
            } else {
                echo "Error: " . $query . "<br>" . $conn->error;
            }

而且我不能真正使用ON DUPLICATE,因为我不知道唯一键,我只知道“products_id”和“languages_id”。

提前致谢。

4

1 回答 1

0

对于这种情况,您可以在 mysql 中使用更简洁和紧凑的语法 - 使用“重复更新”。查询的一般原则将遵循以下内容:-

insert into `TABLE` ( `FIELD1`,`FIELD2`, `FIELD3` ) values ( 'VALUE1','VALUE2','VALUE3' )
on duplicate key
    update
        `FIELD1`='VALUE1',
        `FIELD2`='VALUE2',
        `FIELD1`='VALUE3';

Basing this on original query.
------------------------------

insert into `products_description` 
    (`products_id`, `languages_id`, `products_description_name`, `products_description_short_description`, `products_description_description` )
    values
    ( '$id', '$x', '$varenavn', '$korttekst', '$langtekst' )
    on duplicate key
    update
        `products_id`='$id',
        `languages_id`='$x';
于 2015-07-30T12:09:37.770 回答