注意:PHP中未定义的索引错误消息

时间:2015-11-19 06:38:27

标签: php mysql forms

我为评论系统制作了这个脚本,但我不知道为什么我得到了这个错误信息:

  

注意:未定义的索引:第5行的评论

这是第5行:

$comment = $_POST['comment'];

这是完整的代码:

    <?php 
if(isset($_POST) && array_key_exists('submit',$_POST)){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];

    if (empty($name)){
        $err['name1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Enter 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;'>Write your email correctly!</p>"; 
                }else{
                    if (empty($comment)){
                        $err['comment1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Write your comment down</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 should 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 ('".$_SESSION['pro_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;'>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 = "<p style='color:green;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Your comment will be saved</p></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>

    <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>
    <p><?php if(isset($succ)) echo $succ; ?></p>
</form>

注意:我已阅读所有重复的问题并在我的代码中进行测试,但我没有得到结果&amp;这就是我发布这个问题的原因。我添加了array_key_exists并测试了$comment = (isset($_POST['comment']));,但它不起作用。我整天面对这个问题。所以如果你知道我怎么能解决这个问题,请告诉我,我将不胜感激......提前致谢!

5 个答案:

答案 0 :(得分:5)

因为two name attribute

中有textarea
<textarea name='' name='comment' placeholder='Comment'></textarea>

这将是

<textarea name='comment' placeholder='Comment'></textarea>

答案 1 :(得分:3)

  

注意:未定义的索引:第5行的评论

     

$ _ POST

中没有索引名称&#34;评论&#34; 时出错

检查表单元素

<textarea name='' name='comment' placeholder='Comment'></textarea>

需要名称=&#34;&#34;不是名字=&#39;评论&#39;

更改

<textarea name='comment' placeholder='Comment'></textarea>

答案 2 :(得分:2)

删除额外名称

 <textarea name='' name='comment' placeholder='Comment'></textarea>
           ^     ^

更正代码

<textarea name='comment' placeholder='Comment'></textarea>

答案 3 :(得分:1)

当您使用名称两次时,您在以下行中出现错误,并首先将其作为textarea的空名称:

<textarea name='' name='comment' placeholder='Comment'></textarea>

可能你想这样做:

<textarea name='comment' placeholder='Comment'></textarea>

答案 4 :(得分:0)

在访问之前必须检查post变量,这样可以防止脚本崩溃

if(isset($_POST['comment']))
       $comment= $_POST['comment'];
   else
       $comment="some default value";
  从前端

确保用户已发送评论值   如果您正在编写Web应用程序,则验证java脚本。

重新编写您的代码

<?php 
if(isset($_POST['name']) && isset($_POST['submit']) &&  isset($_POST['email']) && isset($_POST['comment'])){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];

    if (empty($name)){
        $err['name1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Enter 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;'>Write your email correctly!</p>"; 
                }else{
                    if (empty($comment)){
                        $err['comment1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Write your comment down</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 should 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 ('".$_SESSION['pro_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;'>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 = "<p style='color:green;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Your comment will be saved</p></script>";
        }
}else{
$succ = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Missing Params make sure you had filled all necessay fields</p></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>

    <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>
    <p><?php if(isset($succ)) echo $succ; ?></p>
</form>