我得到了用于发布标题和博客正文以及其他一些东西的代码。现在我想使用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 if else语句,但是我不是编程专家,所以出现语法错误。
最佳答案
这可能对您有帮助。
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";
}