表单不会将数据发送到数据库

时间:2016-01-05 10:14:27

标签: php html5 forms mysqli

我正在尝试编写一个表单将一些数据插入到mysql数据库中,并且我填写表单本身。我尝试提交表单,当表单提交但不显示数据库中的任何数据时,我的代码是:

这是我的new_form.php

  <!Doctype html>
        <html>
            <head>
                <title>Form</title>
                <meta charset="utf-8">
                <link rel="stylesheet" type="text/css" href="vendors/bootstrap/css/bootstrap.css">
                <script type="text/javascript" src="vendors/bootstrap/js/bootstrap.js"></script>
                <script type="text/javascript" src="vendors/js/jquery.js"></script>
            </head>

            <body>
                <div class="container">
                <h1>Contact Form</h1>
                    <form method="post" action="form_process.php" class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="name" class="control-label col-sm-2">Name</label>
                            <div class="col-sm-5">
                                <input type="text" name="name" id="name" class="form-control" placeholder="Your name" required >
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="email" class="control-label col-sm-2">E-mail Address</label>
                            <div class="col-sm-5">
                                <input type="email" name="email" id="email" class="form-control" placeholder="Your E-mail" required >
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="subject" class="control-label col-sm-2">Subject</label>
                            <div class="col-sm-5">
                                <input type="text" name="subject" id="subject" class="form-control" placeholder="Your subject" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-2">Select your Gender </label>
                            <div class="col-sm-5">
                                <select class="form-control" name="gender" required>
                                    <option value="">your Gender </option>
                                    <option value="male">Male</option>
                                    <option value="female">Female</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-2">Skills</label>
                            <div class="col-sm-5">
                                <label class="checkbox-inline"><input type="checkbox" name="skill1" value="html">HTML</label>
                                <label class="checkbox-inline"><input type="checkbox" name="skill2" value="php">PHP</label>             
                                <label class="checkbox-inline"><input type="checkbox" name="skill3" value="javascript">JAVASCRIPT</label>               
                                <label class="checkbox-inline"><input type="checkbox" name="skill4" value="css">CSS</label>             
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-2">Select your country</label>
                            <div class="col-sm-5">
                                <select class="form-control" name="country">
                                    <option value="">Select your country</option>
                                    <option value="russia">Russia</option>
                                    <option value="usa">USA</option>
                                    <option value="uk">UK</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="comments" class="control-label col-sm-2">Comments</label>
                            <div class="col-sm-5">
                                <textarea type="text-area" name="comments" id="comments" rows="5" class="form-control"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-5 col-sm-offset-2">
                                <input type="submit" name="submit" class="btn btn-default btn-block" value="submit form">
                            </div>
                        </div>    
                    </form>
                </div>
            </body>
        </html>

这是我的form_process.php

    <?php
        include 'include/connection.php';
        if (isset($_POST['submit'])) {
            $name = strip_tags($_POST['name']);
            $email =  strip_tags($_POST['email']);
            $subject = htmlspecialchars($_POST['subject']);
            $gender = $_POST['gender'];
            $country = $_POST['country'];
            $comments = trim(htmlspecialchars($_POST['comments']));

            $skill1 = $_POST['skill1'] = '';

            $skill2 = $_POST['skill2'] = '';

            $skill3 = $_POST['skill3'] = '';

            $skill4 = $_POST['skill4'] = '';


            $query = "INSERT INTO `php_course`.`comments` (`id`, `name`, `email_address`, `subject`, `gender`, `skill1`, `skill2`, `skill3`, `skill4`, `country`, `comments`) VALUES (NULL, '$name', '$email', '$subject', '$gender', '$skill1', '$skill2', '$skill3', '$skill4', '$country', '$comments') ";

            $query = mysqli_query($con,$query);

            mysqli_close($con);
    } 
?>

这是一个新的工作

<?php
    include 'include/connection.php';

    if (isset($_POST['submit'])) {
        $name = mysqli_real_escape_string($con,strip_tags($_POST['name']));
        $email =  mysqli_real_escape_string($con,strip_tags($_POST['email']));
        $subject = mysqli_real_escape_string($con,htmlspecialchars($_POST['subject']));
        $gender = $_POST['gender'];
        $country = $_POST['country'];
        $comments = mysqli_real_escape_string($con,trim(htmlspecialchars($_POST['comments'])));

        $skill1 = isset($_POST['skill1']) ? $_POST['skill1'] : '';  
        $skill2 = isset($_POST['skill2']) ? $_POST['skill2'] : '';  
        $skill3 = isset($_POST['skill3']) ? $_POST['skill3'] : '';  
        $skill4 = isset($_POST['skill4']) ? $_POST['skill4'] : '';  


        $query = "INSERT INTO comments (id, name, email_address, subject, gender, skill1, skill2, skill3, skill4, country, comments) VALUES (NULL,'$name','$email', '$subject', '$gender', '$skill1', '$skill2', '$skill3','$skill4', '$country', '$comments') ";

        $query = mysqli_query($con,$query); 

        mysqli_close($con);
    } 
?>

我仍然想知道旧的有什么问题:

1 个答案:

答案 0 :(得分:-1)

在form_process.php

<?php 

 Include("connection.php"); 

?>

我认为连接文件未正确包含在内。

首先尝试打印变量。