开发者

PHP MySQL - Count how many articles by a user

I have an application where my users can create something like an article. Now, I wan't to limit the amount of articles written by each user.

I just don't know how to do this - I could imagine that I should use: mysql_count_num_rows or something like that.

In my database I have a table called: opslag, where I have userid_bywho and user_bywho. I imagine that I should count how many times in the table a users ID is found, and if it's above 5 the user should not be able to create the article.

I have the users ID's in a SESSION.

I hope you can help me how to count how many times an ID is shown.

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.";
                }
            }


    $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.";
                    }
                }


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

Then fetch the result if ($fetch > 5 ) { echo 'oops you posted more than 5!' }


Don't use mysql_count_num_rows, do it with SQL assuming that ID is numeric:

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

note: Be careful with SQL injection, escape it, prepare and execute the sentence.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜