PHP MySQL - 计算用户的文章数量

时间:2011-07-26 19:06:33

标签: php mysql

我有一个应用程序,我的用户可以创建类似文章的内容。现在,我不想限制每个用户写的文章数量。

我只是不知道该怎么做 - 我可以想象我应该使用:mysql_count_num_rows或类似的东西。

在我的数据库中,我有一个名为:opslag的表,其中我有userid_bywho和user_bywho。我想我应该计算表中找到用户ID的次数,如果它高于5,则用户不应该能够创建该文章。

我在SESSION中有用户ID。

我希望你能帮我知道如何计算ID的显示次数。

if(isset($_POST['title']) && isset($_POST['daystart']) && isset($_POST['address']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['website']) && isset($_POST['content']))
            {

                if($_POST['title'] === ''){
                    $errMsg = "Du skal udfylde titel";
                }
                elseif($_POST['daystart'] === ''){
                    $errMsg = "Du skal udfylde start dag";
                }
                elseif($_POST['address'] === ''){
                    $errMsg = "Du skal udfylde adresse";
                }
                elseif($_POST['phone'] === ''){
                    $errMsg = "Du skal udfylde telefon";
                }
                elseif($_POST['email'] === ''){
                    $errMsg = "Du skal udfylde email";
                }
                elseif($_POST['website'] === ''){
                    $errMsg = "Du skal udfylde website";
                }
                elseif($_POST['content'] === ''){
                    $errMsg = "Du skal udfylde beskrivelse";
                } else {
                $sql = mysql_query("INSERT INTO opslag (userid_bywho, user_bywho, title, category, link, daystart, address, phone, email, website, content)VALUES('$userid_bywho', '$user_bywho', '$title', '$category', '$link', '$daystart', '$address', '$phone', '$email', '$website', '$content')")or die(mysql_error());
                $add_success = "Dit opslag er oprettet.";
                }
            }

3 个答案:

答案 0 :(得分:4)

    $user_id=$_SESSION['user_id'];
    $num=10; //default he can't create the article
    if (!is_numeric($user_id)) die('SQL INJECTION!');
    $sql="SELECT userid_bywho, count(*) as num FROM opslag WHERE userid_bywho=$user_id GROUP BY userid_bywho";
        $result = mysql_query($sql);
        if (!$result) {
          echo "can't run query";
        } else {
           $row = mysql_fetch_row($result);
           $num = $row[1];
        }

if ($num>=5 ){
$errMsg = "You have more than 5 articles";
} elseif(isset($_POST['title']) && isset($_POST['daystart']) && isset($_POST['address']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['website']) && isset($_POST['content']))
                {

                    if($_POST['title'] === ''){
                        $errMsg = "Du skal udfylde titel";
                    }
                    elseif($_POST['daystart'] === ''){
                        $errMsg = "Du skal udfylde start dag";
                    }
                    elseif($_POST['address'] === ''){
                        $errMsg = "Du skal udfylde adresse";
                    }
                    elseif($_POST['phone'] === ''){
                        $errMsg = "Du skal udfylde telefon";
                    }
                    elseif($_POST['email'] === ''){
                        $errMsg = "Du skal udfylde email";
                    }
                    elseif($_POST['website'] === ''){
                        $errMsg = "Du skal udfylde website";
                    }
                    elseif($_POST['content'] === ''){
                        $errMsg = "Du skal udfylde beskrivelse";
                    } else {
                    $sql = mysql_query("INSERT INTO opslag (userid_bywho, user_bywho, title, category, link, daystart, address, phone, email, website, content)VALUES('$userid_bywho', '$user_bywho', '$title', '$category', '$link', '$daystart', '$address', '$phone', '$email', '$website', '$content')")or die(mysql_error());
                    $add_success = "Dit opslag er oprettet.";
                    }
                }

答案 1 :(得分:0)

SELECT COUNT(*)FROM opslag WHERE userid = _bywho = USERID

然后获取结果     if ($fetch > 5 ) { echo 'oops you posted more than 5!' }

答案 2 :(得分:0)

不要使用mysql_count_num_rows,假设ID是数字,请使用SQL:

 $sql = "select count(*) from table 
   where {$_SESSION['ID']} in ( userid_bywho , user_bywhouser_id)";

注意:注意SQL注入,转义它,准备并执行句子。