Php 设置post的最小字符长度
我得到了发布标题和博客正文的代码,还有一些其他的东西。现在我想使用strlen设置标题和正文的最小字符长度,我的代码是Php 设置post的最小字符长度,php,syntax,blogs,strlen,Php,Syntax,Blogs,Strlen,我得到了发布标题和博客正文的代码,还有一些其他的东西。现在我想使用strlen设置标题和正文的最小字符长度,我的代码是 if(isset($_POST['submit'])) { $title=$_POST['title']; $body=$_POST['body']; $category=$_POST['category']; $title = mysql_real_escape_string($title); $body = mysql_real_escape
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语句,但我不是编程专家,所以我遇到了语法错误。如果您想计算字符串的长度,可以使用“strlen” 这可能会对你有所帮助
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";
}
if(isset($\u POST['submit'])){
$title=$_POST['title'];
如果(斯特伦($title)80))
{
$query=mysql_query(“插入到博客(id、标题、正文、发布者、bio、类别id、发布者)值('$id'、'$title'、'$body'、'$posted_by'、'$bio'、'$category'、'$date');
}
否则{
$query=0;
}
如果($query){
回声“张贴”;
}
否则{
回声“错误”;
}
}否则{
回波“数据丢失”;
}
if(strlen($title)您尝试了什么?您想要的$title
和$body
的最低字符要求是什么?我尝试了类似的代码if(strlen($title)<10){echo“title不能少于10个字符”}否则if(strlen($body)<500){echo“正文不得少于500个字符”;}否则{}
但语法错误您发布的代码没有问题。尝试粘贴语法错误,您会看到一个,我收到了关于最小字符数的消息,但随后检查了我的数据库,并且他们的博客已添加到表中。您是否可以对此提供帮助。发生这种情况的原因是,如果没有执行查询,变量$query将不会被删除t、 只需在query下设置一个else块。$query=0;再次查看修改后的代码。现在,如果出现任何错误,它将打印“error”。现在,即使满足所有条件,它也只是一个错误
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";
}
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.";
}