无法将表单提交到数据库

时间:2017-08-14 09:40:19

标签: php html mysql sql mysqli

我正在尝试创建一个简单的表单,使用户能够将数据提交到我的数据库中。但数据库中没有任何记录。我不知道我的代码有什么问题。我阅读了各种各样的问题,但我似乎没有得到任何解决方案。我尝试使用不同的方法,但它只是不起作用。它连接到数据库就好了但我提交代码时数据库中没有任何内容 这是html

<div id="table">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <form name="register" action="process.php" method="post" role="form">
                <div class="form-group">
                    <input type="text" name="firstName" class="form-control input-text" id="firstName" placeholder="Your First Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
                    <div class="validation"></div>
                </div>
                <div class="form-group">
                    <input type="text" name="lastName" class="form-control input-text" id="lastName" placeholder="Your Last Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
                    <div class="validation"></div>
                </div>
                <div class="form-group">
                    <input type="email" class="form-control input-text" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
                    <div class="validation"></div>
                </div>
                <div class="form-group">
                    <input type="text" class="form-control input-text" name="phone" id="phone" placeholder="Your Phone Number" data-msg="Please enter a valid Number" />
                    <div class="validation"></div>
                </div>
                <div class="form-group">
                  <select required class="form-control" name="gender" id="gender">
                    <option value="" disabled selected>Select Gender</option>
                    <option value="male">Male</option>
                    <option value="femal">Female</option>
                  </select>
                </div>
                <div class="form-group">
                  <select required class="form-control" name="course" id="course">
                    <option value="" disabled selected hidden>Select First Course</option>
                    <option value="excel">Microsoft Excel</option>
                    <option value="web">Web Development</option>
                  </select>
                </div>
                <div class="form-group">
                  <select class="form-control" name="courses" id="courses">
                    <option value="" disabled selected hidden>Select Second Course</option>
                    <option value="micro">Microsoft Excel</option>
                    <option value="deve">Web Development</option>
                  </select>
                </div>
                <div class="form-group">
                    <input type="text" class="form-control input-text" name="occupation" id="occupation" placeholder="Occupation" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
                    <div class="validation"></div>
                </div>
                <div class="form-group">
                    <textarea class="form-control textarea" rows="3" name="message" id="message" placeholder="What are your expectations for the course" data-rule="minlen:20" data-msg="Please enter at least 20 chars of subject"></textarea>
                    <div class="validation"></div>
                </div>
                <div class="form-group">
                    <span>
                        <input type="checkbox" name="checkbox" aria-label="agree" value="check">
                    </span>
                    <p>By clicking on submit, you have agreed to the <a href="terms.html">terms and conditions</a> the program</p>
                    <div class="validation"></div>
                </div>

                <div class="text-center"><button type="submit" class="input-btn" name="submit" onclick="if(!this.form.checkbox.checked){alert('You must agree to the terms first.');return false}"  />Submit</button></div>
                </div>
            </form>
        </div>
    </div>
</div>

这是php

    <?php
    function Connect() {
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "Kpontsubless12.";
    $dbname = "register";

    $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);
    return $conn;
}

// create a variable
if (isset($_POST['submit'])) { 
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$gender=$_POST['gender'];
$course=$_POST['course'];
$courses=$_POST['courses'];
$occupation=$_POST['occupation'];
$message=$_POST['message'];
$checkbox=$_POST['checkbox'];

$querye = ("INSERT INTO students (firstName,lastName,email,phone,gender,course,courses,occupation,message,checkbox)
             VALUES('$firstName','$lastName','$email','$phone','$gender','$course','$courses','$occupation,'$message','checkbox')");
             $sql=mysqli_query($conn,$querye);
    mysqli_close($conn);

}
?>

6 个答案:

答案 0 :(得分:3)

在查询之前执行此操作

$conn = Connect();
$querye = ("INSERT INTO students (firstName,lastName,email,phone,gender,course,courses,occupation,message,checkbox)
         VALUES('$firstName','$lastName','$email','$phone','$gender','$course','$courses','$occupation,'$message','checkbox')");
         $sql=mysqli_query($conn,$querye);

在您查询之前,我所做的就是您的$conn = Connect();功能。

答案 1 :(得分:0)

确保$ conn不为空,你没有调用连接函数

答案 2 :(得分:0)

您不要在代码中调用Connect()函数。

答案 3 :(得分:0)

你创建了连接功能,但在执行查询时忘了调用它。 尝试

       <?php
        function Connect() {
        $dbhost = "localhost";
        $dbuser = "root";
        $dbpass = "Kpontsubless12.";
        $dbname = "register";

        $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);
        return $conn;
    }

    // create a variable
    if (isset($_POST['submit'])) { 
    $firstName=$_POST['firstName'];
    $lastName=$_POST['lastName'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $gender=$_POST['gender'];
    $course=$_POST['course'];
    $courses=$_POST['courses'];
    $occupation=$_POST['occupation'];
    $message=$_POST['message'];
    $checkbox=$_POST['checkbox'];

 $conn=connect();
    $querye = ("INSERT INTO students (firstName,lastName,email,phone,gender,course,courses,occupation,message,checkbox) VALUES('$firstName','$lastName','$email','$phone','$gender','$course','$courses','$occupation,'$message','checkbox')");
                 $sql=mysqli_query($conn,$querye);
        mysqli_close($conn);

    }
    ?>

答案 4 :(得分:0)

在您$querye=..........$conn = connect();

之前

答案 5 :(得分:0)

在您的代码中,您需要解决许多问题。

首先,您没有启动连接。为此,您需要执行以下操作。

    <?php
    function Connect() {
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "Kpontsubless12.";
    $dbname = "register";

    $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);
    return $conn;
}

// create a variable
if (isset($_POST['submit'])) { 
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$gender=$_POST['gender'];
$course=$_POST['course'];
$courses=$_POST['courses'];
$occupation=$_POST['occupation'];
$message=$_POST['message'];
$checkbox=$_POST['checkbox'];

$conn = Connect();

$querye = ("INSERT INTO students (firstName,lastName,email,phone,gender,course,courses,occupation,message,checkbox)
             VALUES('$firstName','$lastName','$email','$phone','$gender','$course','$courses','$occupation,'$message','checkbox')");
             $sql=mysqli_query($conn,$querye);
    mysqli_close($conn);

}
?>

现在已经不在了,你需要在你的sql中放弃使用变量,这会打开你的代码到SQL注入漏洞。开始在你的mysqli_中使用预备语句,似乎你开始编写代码,以便更好地养成使用预准备语句来保护数据的习惯。

还要看PDO快速干净可爱。