通过一个mysql查询更新多行

时间:2015-02-19 07:44:20

标签: php mysql

我试图通过一个mysql更新查询更新多行。这是我的代码。但它没有工作。显示错误。我试图更新开始和结束字段mysql表

$resname=$_POST['resname'];
$rw1start=$_POST['rw1start'];
$rw1end=$_POST['rw1end'];
$rw2start=$_POST['rw2start'];
$rw2end=$_POST['rw2end'];
$rw3start=$_POST['rw3start'];
$rw3end=$_POST['rw3end'];
$rw4start=$_POST['rw4start'];
$rw4end=$_POST['rw4end'];
$rw5start=$_POST['rw5start'];
$rw5end=$_POST['rw5end'];
$rw6start=$_POST['rw6start'];
$rw6end=$_POST['rw6end'];
$rw7start=$_POST['rw7start'];
$rw7end=$_POST['rw7end'];

    $sql="UPDATE opening 
            SET start = (case when day = 'Monday' then '$rw1start'
                             when day = 'Tuesday' then '$rw2start'
                             when day = 'Wednesday' then '$rw3start'
                              when day = 'Thursday' then '$rw4start'
                               when day = 'Friday' then '$rw5start'
                                when day = 'Saturday' then '$rw6start'
                                 when day = 'Sunday' then '$rw7start'
                        end),
            SET end = (case when day = 'Monday' then '$rw1end'
                             when day = 'Tuesday' then '$rw2end'
                             when day = 'Wednesday' then '$rw3end'
                              when day = 'Thursday' then '$rw4end'
                               when day = 'Friday' then '$rw5end'
                                when day = 'Saturday' then '$rw6end'
                                 when day = 'Sunday' then '$rw7end'
                        end)        


                    WHERE (day in 'Monday', 'Tuesday', 'Wednesday','Thursday','Friday','Saturday','Sunday') AND res_name='$resname'";

    $result=mysql_query($sql)or die ("Error");

opening table

1 个答案:

答案 0 :(得分:0)

您的更新命令不正确,此外您还保留了列名end。你需要反击它。您已使用set 2次,但在更新语句中,您只需要一次

update table set col1 = 'someval',col2='someval' 


$sql="UPDATE opening 
SET start = (
               case 
               when day = 'Monday' then '$rw1start'
               when day = 'Tuesday' then '$rw2start'
               when day = 'Wednesday' then '$rw3start'
               when day = 'Thursday' then '$rw4start'
               when day = 'Friday' then '$rw5start'
               when day = 'Saturday' then '$rw6start'
               when day = 'Sunday' then '$rw7start'
               end
             ),
     `end` = (
               case 
               when day = 'Monday' then '$rw1end'
               when day = 'Tuesday' then '$rw2end'
               when day = 'Wednesday' then '$rw3end'
               when day = 'Thursday' then '$rw4end'
               when day = 'Friday' then '$rw5end'
               when day = 'Saturday' then '$rw6end'
               when day = 'Sunday' then '$rw7end'
               end
             ) 
           WHERE day in ('Monday', 'Tuesday',  'Wednesday','Thursday','Friday','Saturday','Sunday') 
           AND res_name='$resname'";