-3

这个 sql :

if($_POST['adat']){
    mysql_query("UPDATE pages SET page_value = '0' WHERE user_id = '$_SESSION[user_id]' ");


    foreach($_POST['page_ids'] as $page){
        $pa = $page;

        if ($pa){
            mysql_query("UPDATE pages SET page_value = '1' WHERE user_id = '$_SESSION[user_id]'  AND page_id = '$page'  LIMIT 2 ");
        }

    }   
}

I like, that When selected page, then if more page selected even just let it be two update? 我尝试order by user_id limit 2,或者简单limit 2,但这不起作用。

4

2 回答 2

1

只需这样做

mysql_query("UPDATE pages SET page_value = '1' WHERE user_id = '$_SESSION[user_id]'  AND page_id FIND_IN_SET($_POST['page_ids'])");

无需循环执行。

于 2013-07-25T07:03:42.543 回答
1

而不是限制

if($_POST['adat']){
    mysql_query("UPDATE pages SET page_value = '0' WHERE user_id = '$_SESSION[user_id]' ");


    foreach($_POST['page_ids'] as $page){
        $pa = $page;

        if ($pa){
            mysql_query("UPDATE pages SET page_value = '1' WHERE user_id = '$_SESSION[user_id]'  AND page_id = '$page'  LIMIT 2 ");
        }

    }
}

你必须在循环后面使用 break 关键字

if($_POST['adat']){
    mysql_query("UPDATE pages SET page_value = '0' WHERE user_id = '$_SESSION[user_id]' ");

    $i=1;
    foreach($_POST['page_ids'] as $page){
        $pa = $page;

        if ($pa){
            mysql_query("UPDATE pages SET page_value = '1' WHERE user_id = '$_SESSION[user_id]'  AND page_id = '$page'");
        }
     if($i>=2){
        break;
     }  
     $i++;
    }
}
于 2013-07-25T07:11:32.920 回答