我制作了一个php评论系统,但似乎代码是正确的&我也没有收到任何错误消息。
这是我的代码:
<?php
$con = mysqli_connect("localhost","root","","ecommerce");
$errors = array();
if (isset($_POST['submit'])){
$name = (isset($_POST['name']));
$email = (isset($_POST['email']));
$comment = (isset($_POST['comment']));
if (empty($name)){
$err['name1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Please write down your name!</p>";
}else{
$name_length = strlen($name);
if ($name_length > 2 ){
if (!empty($email)){
$email_verification = test_input($email);
if (!filter_var($email_verification, FILTER_VALIDATE_EMAIL)) {
$err['email1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Your email is not written correctly!</p>";
}else{
if (empty($comment)){
$err['comment1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Please write your comment!</p>";
}else{
$comment_length = strlen($comment);
if (($comment_length < 5) || ($comment_length > 100)){
$err['comment2'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Your comment must be between 5 to 100 characters!</p>";
}else{
$que = $con->query("SELECT * FROM product_comments WHERE productcode='%s'", $_SESSION['pro_id']);
if(mysqli_num_rows($que) == 0){
$insert_id2 = "insert into product_comments (productcode) values ('$product_id')";
$insert_id2 = mysqli_query($con, $insert_id2);
}
}
}
}
}else{
$err['email2'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Please write your email down!</p>";
}
}else{
$err['name2'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Your name must be more than 2 words!</p>";
}
}
//check errors
if(count($err) == 0)
{
$insert_comment = "insert into product_comments (email,name,comment,modified_date, modified_time) values ('$email_verification','$name','$comment',CURDATE(), CURTIME())";
$insert_comment = mysqli_query($con, $insert_comment);
$succ = "<script>alert('Your comment is submitted correctly')</script>";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action='' method='POST'>
<span>
<input type='text' name='name' placeholder='Your Name'/>
<input type='email' name='email' placeholder='Email Address'/>
</span>
<textarea name='' name='comment' placeholder='Comment'></textarea>
<input name='submit' style='font-family: BJadidBold, Arial, Helvetica, sans-serif;' type='submit' value='Submit' class='btn btn-default pull-right'></input>
</form>
<p><?php if(isset($err['name1'])) echo $err['name1']; ?></p>
<p><?php if(isset($err['name2'])) echo $err['name2']; ?></p>
<p><?php if(isset($err['email1'])) echo $err['email1']; ?></p>
<p><?php if(isset($err['email2'])) echo $err['email2']; ?></p>
<p><?php if(isset($err['comment1'])) echo $err['comment1']; ?></p>
<p><?php if(isset($err['comment2'])) echo $err['comment2']; ?></p>
每次我想提交包含正确信息的表单时,我都会收到 $ err ['name2'] 错误消息
您的姓名必须超过2个字
我不知道为什么会这样,所以如果你知道它有什么问题请告诉我...谢谢!
答案 0 :(得分:1)
这不符合你的想法:
$name = (isset($_POST['name']));
isset()
返回一个布尔值。它不会返回它正在检查的东西的值。这意味着$name
不会包含您所期望的内容,从而导致此评估为false
:
if ($name_length > 2 ){
(我的猜测是$name
在被解释为字符串时最终包含0或1,因此strlen($name)
解析为1
。)
尝试设置值时删除isset()
:
$name = $_POST['name'];