【问题标题】:Setting minimum length of characters for post设置帖子的最小字符长度
【发布时间】:2015-09-13 08:37:30
【问题描述】:

我获得了发布博客标题和正文以及其他一些内容的代码。现在我想使用 strlen 设置标题和正文的最小字符长度,我的代码是

 if(isset($_POST['submit'])) {
   $title=$_POST['title'];
   $body=$_POST['body'];
   $category=$_POST['category'];
   $title = mysql_real_escape_string($title);
   $body = mysql_real_escape_string($body);
   $posted_by = $user;
   $bio = $bio;
   $id=$_SESSION['id'];
   $date = date ('Y-m-d');
   $body = htmlentities($body);
   if ($title && $body && $category) {
      $query = mysql_query("INSERT INTO blogs (id, title, body, posted_by, bio, category_id, posted) VALUES ('$id', '$title', '$body', '$posted_by','$bio', '$category', '$date')");
      if($query) {
        echo "posted";
      }
      else {
        echo "error";
      }
   }else {
    echo "data missing";
   }
}

此代码工作正常,只是想插入标题和正文的最少字符要求。我用 strlen 尝试了一些 if else 语句,但我不是编程专家,所以我遇到了语法错误。

【问题讨论】:

  • 你尝试了什么?您希望$title$body 的最小字符数是多少?

标签: php syntax blogs strlen


【解决方案1】:

这可能对你有帮助..

 if(isset($_POST['submit'])) {
           $title=$_POST['title'];
           if(strlen($title)<8)
           {
               //codes to handle error
           }

   $body=$_POST['body'];
   if(strlen($body)<80)
           {
               //codes to handle error
           }
   $category=$_POST['category'];
   $title = mysql_real_escape_string($title);
   $body = mysql_real_escape_string($body);
   $posted_by = $user;
   $bio = $bio;
   $id=$_SESSION['id'];
   $date = date ('Y-m-d');
   $body = htmlentities($body);
   if ($title && $body && $category) {
      if(strlen($title)>8 && strlen($body>80))
           {

      $query = mysql_query("INSERT INTO blogs (id, title, body, posted_by, bio, category_id, posted) VALUES ('$id', '$title', '$body', '$posted_by','$bio', '$category', '$date')");
           }
        else {
                  $query = 0;
              }
      if($query) {
        echo "posted";

      }
      else {
        echo "error";
      }
   }else {
    echo "data missing";
   }

【讨论】:

  • 不错,我收到了最少字符数的消息,但随后检查了我的数据库,他们的博客已添加到表中。你能帮忙吗?
  • 发生这种情况的原因是,如果没有执行查询,则不会设置变量 $query。只需在查询下设置一个 else 块。 $查询 = 0;再次查看修改后的代码。现在,如果出现任何问题,它将打印“错误”。
  • 现在即使满足所有条件也只是错误
【解决方案2】:

如果要计算字符串的长度,可以使用“strlen”

if(strlen($title)>5 && strlen($body)>5) {
  // $title and $body is more than 5 bytes long
}else{
  // $title or $body is 5 or less than 5 bytes long
}

Documentation

【讨论】:

  • 我尝试了类似的代码if (strlen($title) &lt; 10) { echo "title must not be less than 10 characters"; } else if (strlen($body) &lt; 500) { echo "body must not be less than 500 characters"; } else { },但语法错误
  • 您发布的代码没有任何问题。尝试粘贴您看到的语法错误
【解决方案3】:
if(strlen($title)<=15 && strlen($body)<=500) {
   $query = mysql_query("INSERT INTO blogs (id, title, body, posted_by, bio, category_id, posted) VALUES ('$id', '$title', '$body', '$posted_by','$bio', '$category', '$date')");
      if($query) {
        echo "posted";
      }
}
else
{
echo "Minimum Character Requirments failed.";
}

【讨论】:

    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 2021-09-18
    • 2022-12-18
    • 1970-01-01
    • 2013-06-28
    • 2012-04-27
    • 1970-01-01
    相关资源
    最近更新 更多