如果(isset)$ _ POST麻烦

时间:2015-12-29 22:10:20

标签: php

我真的在与此作斗争,我已经尝试了所有关于此问题的youtube / google页面,似乎无法解决问题。

点击提交时,数据库只收集'idContactForm'而不收集其余的(名称/电子邮件/主题/消息)

我已经尝试了使用if(isset)$ _ POST行找到的所有内容,但我似乎总是得到“$ name / $ email / $ subject / $ message”中的“未定义索引:”错误

我做错了什么?

 <?php

    include 'config.php';

    if (!isset($_POST['submitted'])) {

        $name = trim(mysqli_escape_string($conn,$_POST['Name']));
        $email = trim(mysqli_escape_string($conn,$_POST['Email']));
        $subject = trim(mysqli_escape_string($conn,$_POST['Subject']));
        $message = trim(mysqli_escape_string($conn,$_POST['Message']));

    } else {
    ## connect mysql server
        $mysqli = new mysqli('localhost', 'root', '******', 'tomepublications');
        # check connection
        if ($mysqli->connect_errno) {
            echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
            exit();
        }


            # insert data into mysql database
            $sql = "INSERT  INTO `contactform` (`idContactForm`, `Name`, `Email`, `Subject`, `Message`) 
                    VALUES (NULL, '{$name}', '{$email}', '{$subject}', '{$message}')";

            if ($mysqli->query($sql)) {
                //echo "New Record has id ".$mysqli->insert_id;
                echo "<p>Registred successfully!</p>";
            } else {
                echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
                exit();
            }
        }

    ?>  

HTML

 <form method="post" action="contactf.php">
                                    <div class="form-group">
                                        <input type="text" name="name" id="Name" class="form-control" placeholder="Name" required>
                                    </div>
                                    <div class="form-group">
                                        <input type="email" name="email" id="Email"class="form-control" placeholder="Email" required>
                                    </div>
                                    <div class="form-group">
                                        <input type="text" name="subject" id="Subject"class="form-control" placeholder="Subject" required>
                                    </div>
                                    <div class="form-group">
                                        <textarea name="message" class="form-control" id="Message" rows="15" placeholder="Message" required></textarea>
                                    </div>
                                    <input type="hidden" name="submitted" value="true">
                                    <input type="submit" value="submit" class="btn btn-primary">Send Message</>
                                </form>

3 个答案:

答案 0 :(得分:3)

变量区分大小写。您的表单使用name="name",全部为小写,但您查找的是$_POST['Name']大写 N

答案 1 :(得分:2)

您的错误,$ name,$ email,$ subject和$ message不在其他范围内。

<?php

include 'config.php';

if (isset($_POST['submitted'])) {

    ## connect mysql server
    $mysqli = new mysqli('localhost', 'root', '******', 'tomepublications');
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $name = trim(mysqli_escape_string($conn,$_POST['name']));
    $email = trim(mysqli_escape_string($conn,$_POST['email']));
    $subject = trim(mysqli_escape_string($conn,$_POST['subject']));
    $message = trim(mysqli_escape_string($conn,$_POST['message']));


    # insert data into mysql database
    $sql = "INSERT  INTO `contactform` (`idContactForm`, `Name`, `Email`, `Subject`, `Message`) 
                VALUES (NULL, '{$name}', '{$email}', '{$subject}', '{$message}')";

        if ($mysqli->query($sql)) {
            //echo "New Record has id ".$mysqli->insert_id;
            echo "<p>Registred successfully!</p>";
        } else {
            echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
            exit();
        }
    }

?>  

答案 2 :(得分:1)

在else块中构建SQL查询时,这些变量($ name,$ email,$ subject,$ message)仍未定义。变量仅在if块中定义。