我做了一個php評論系統,但它似乎代碼都沒事&我也沒有得到任何錯誤消息。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 [「名2」]錯誤是
您的名字留言必須超過2個字
我不知道爲什麼發生這種情況,所以如果你知道它有什麼問題,請讓我知道......謝謝!
'(isset($ _ POST ['var']))'這不是它的用法。你可能試圖使用三元語法。還要確保你開始了會話。 –
*「我也沒有收到任何錯誤訊息」* - 可能是因爲您沒有找到它們。請參閱以下鏈接http://php.net/manual/en/mysqli.error.php和http://php.net/manual/en/function.error-reporting.php 並將其應用於您的代碼。 –
更改'$ name =(isset($ _ POST ['name'])); $ email =(isset($ _ POST ['email'])); $ comment =(isset($ _ POST ['comment']));'to'$ name = $ _POST ['name']; $ email = $ _POST ['email']; $ comment = $ _POST ['comment'];' –