[英]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. 我只是不知道该怎么做 - 我可以想象我应该使用:mysql_count_num_rows或类似的东西。
In my database I have a table called: opslag, where I have userid_bywho and user_bywho. 在我的数据库中,我有一个名为:opslag的表,其中我有userid_bywho和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. 我想我应该计算表中找到用户ID的次数,如果它高于5,则用户不应该能够创建该文章。
I have the users ID's in a SESSION. 我在SESSION中有用户ID。
I hope you can help me how to count how many times an ID is shown. 我希望你能帮助我如何计算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.";
}
}
$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 SELECT COUNT(*)FROM opslag WHERE userid = _bywho = USERID
Then fetch the result if ($fetch > 5 ) { echo 'oops you posted more than 5!' }
然后获取结果if ($fetch > 5 ) { echo 'oops you posted more than 5!' }
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: 不要使用mysql_count_num_rows,假设ID是数字,请使用SQL:
$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. 注意:注意SQL注入,转义它,准备并执行句子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.