比如文章发布模块,要检查发布文章是否重复,找到article模块所在文件夹,
找到文章发布的处理文件或判断文件,一般都是inc.php或.php文件。
比如子目录member下的postarticle.php有以下判断性代码:

empty($post['classid']) && redirect('文章分类未选择,请返回选择。');
    empty($post['subject']) and redirect('文章标题未填写,请返回填写。');
    empty($post['content']) and redirect('文章内容未填写,请返回填写。');

我们可以在前面添加一段:

  //标题重复检查
   if($db->get_one("SELECT * FROM `{$dbpre}articles` WHERE subject='$post[subject]'")){
        redirect("此标题【 '$post[subject]' 】已存在,请更换其他标题。");
		  }

同样,在子目录admin下的article_add.inc.php有代码:

empty($article['subject']) && cpmsg('标题未填写,请返回填写。');
    empty($article['classid']) && cpmsg('分类未选择,请返回选择。');
    empty($article['content']) && cpmsg('内容未填写,请返回填写。');

我们同样可以在前面添加一段。

//标题重复检查
   if($db->get_one("SELECT * FROM `{$dbpre}articles` WHERE subject='$article[subject]'")){
        redirect("此标题【 '$article[subject]' 】已存在,请更换其他标题。");
		  }

如果想更详细地判断到当前分类,可以再后面加一句

AND classid='$article[classid]'

完整代码:

//标题重复检查
   if($db->get_one("SELECT * FROM `{$dbpre}articles` WHERE subject='$article[subject]' AND classid='$article[classid]'")){
        redirect("此分类下的标题【 '$article[subject]' 】已存在,请更换其他标题。");
		  }

需要注意的是,变量名$post[subject],$article[subject],$article[classid]等 是取得当前页面表单中的输入值,和每一条表单项的name值一一对应。
比如:$post[subject],$article[subject]都意为:取得 【input name=”subject” 】表单项的当前输入值。$post或$article由程序设计决定,我们不需要深究,照样搬就好了。(目前不是很明白)

这样的话,以后添加文章,如果标题有重复,就会有错误提示。

原创文章,转载请保留出处。

未经允许请勿转载于都人博客的文章。

标签: , , ,

留下回复

你可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">