基于多个复选框和输入更新数据库中的多个行

时间:2015-02-18 02:45:20

标签: php mysql

我有下面的数组,我需要根据这个更新数据库。 它应该像下面的示例代码,但我不知道如何正确地做到这一点:

UPDATE productPercent SET percent="$percent" WHERE 
store="$store" AND 
startDate>"$start_date" AND 
endDate<"$end_date" AND 
storeGroup="$storeGroup" AND 
productGroup="$product_group" AND 
productName LIKE '$search%'

我需要检查每个商店,商店组,产品(如果包含单词)和产品组,然后更新productPercent表。百分比,产品组,商店组,产品名称和商店位于不同的表中,因此需要某种内部联接。 我需要一些指示,因为我不知道如何开始,谢谢。

Array
(
    [percent] => 3
    [store] => Array
        (
            [0] => 36
            [1] => 45
            [2] => 56
        )

    [start_date] => 2015-02-09
    [end_date] => 2015-03-31
    [storeGroup] => Array
        (
            [0] => 2
            [1] => 4
        )

    [product_group] => Array
        (
            [0] => 13
            [1] => 31
            [2] => 32
        )

    [search] => iphone
    [setPercent] => Submit
)

更新:数据模型 - tableName:columns(连接表)

商店 :id,name,startDate,endDate
storeGroup :id,storeGroupID(在表storeGroupName中:id,name),storeID
productGroup :id,productID(表productName:id,name),groupID(表productGroupName:id,name)
productName :id,name
productPercent :id,productID,storeID,percent

1 个答案:

答案 0 :(得分:0)

$pdoHandle = $this->getPDOHandle();
$searchword = 'iphone';
$sql = "UPDATE 
productPercent 
inner join store on productPercent.storeID=store.id
inner join storeGroup on storeGroup.storeID=store.id
inner join productGroup on productGroup.id=storeGroup.groupID
inner join productName on productPercent.productID=productName.id and productGroup.productID=productName.id

SET percent=:percent
WHERE productName.name like :searchword";


$pdo->prepare($sql);
$pdo->setAttribute('percent', floatval($percent/100));
$pdo->setAttribute('searchword', $searchword . '%');
相关问题