Boostrap表单不会将数据提交到数据库

时间:2017-11-07 18:32:18

标签: php html forms bootstrap-4

我在模态中有一个bootstrap表单,但我无法将数据提交到我的数据库

我的模态表单(html)

<div class="modal-body">

      <form method="post"  action="insertRecord.php">
        <div class="form-group">
          <label for="firstName">First Name</label>
          <input type="text" name="txtFirstName"  class="form-control" id="inputFirstName" placeholder="First Name">
        </div>
        <div class="form-group">
          <label for="lastName">Last Name</label>
          <input type="text" name="txtLastName" class="form-control" id="inputLastName" placeholder="First Name">
        </div>
        <div class="form-group">
          <label for="emailAddress">Email address</label>
          <input type="email" name="txtEmail" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
          <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
        <div class="form-group">
          <label for="password">Password</label>
          <input type="password" name="txtPassword" class="form-control" id="exampleInputPassword1" placeholder="Password">
        </div>
        <div class="form-group">
          <label for="Dob">Date of birth:</label>
          <input type="date" name="DoB" class="form-control" id="inputDOB" placeholder="First Name">
        </div>
        <div class="form-group">
          <label for="Address1">Address1</label>
          <input type="text" name="txtAddress1" class="form-control" id="inputAddress1" placeholder="First Name">
        </div>
        <div class="form-group">
          <label for="Address2">Address2</label>
          <input type="text" name="txtAddress2" class="form-control" id="inputAddress2" placeholder="First Name">
        </div>
        <div class="form-group">
          <label for="postcode">Postcode</label>
          <input type="text" name="txtPostcode" class="form-control" id="inputPostcode" placeholder="First Name">
        </div>
        <input type="submit" value="Submit" name="taxSubmit"  />
      </form>

    </div>

我的代码插入表单信息(php)

<?php

include 'connection.php';

$firstName=$_POST['txtFirstName'];
$lastName=$_POST['txtLastName'];
$email=$_POST['txtEmail'];
$password=$_POST['txtPassword'];
$dob=$_POST['DoB'];
$address1=$_POST['txtAddress1'];
$address2=$_POST['txtAddress2'];
$postcode=$_POST['txtPostcode'];


$query="INSERT INTO Userinfo (FirstName, LastName, 
Email,Password,Dob,Address1,Address2,Postcode) VALUES ('$firstName', 
'$lastName', 
'$email','$password','$dob','$address1','$address2','$postcode')";




mysqli_query($connection, $query);
?>

我有另一个连接到数据库的脚本,我很感激我能得到的任何帮助,因为我已经被困在这个问题好几个小时而且无法解决这个问题。

非常感谢

1 个答案:

答案 0 :(得分:0)

试试这个例子,我跑了并且适合我:

<?php
require 'connection.php';

$firstName = $lastName = $email = $email = $password = $dob = $address1 = $address2 = $postcode = $query = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $firstName = $_POST['txtFirstName'];
    $lastName = $_POST['txtLastName'];
    $email = $_POST['txtEmail'];
    $password = $_POST['txtPassword'];
    $dob = $_POST['DoB'];
    $address1 = $_POST['txtAddress1'];
    $address2 = $_POST['txtAddress2'];
    $postcode = $_POST['txtPostcode'];


    $query = "INSERT INTO Userinfo (FirstName, LastName, 
    Email, Password, Dob, Address1, Address2, Postcode) VALUES ('$firstName', 
    '$lastName', '$email','$password','$dob','$address1','$address2','$postcode')";
}
echo $query;
if (mysqli_query($connection, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query. "<br>" . mysqli_error($connection);
}

mysqli_close($connection);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Insert Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>

        <div class="container">
            <h2>Modal</h2>
            <!-- Trigger the modal with a button -->
            <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

            <!-- Modal -->
            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog">

                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Modal Header</h4>
                        </div>
                        <div class="modal-body">

                            <form method="post"  action="">
                                <div class="form-group">
                                    <label for="firstName">First Name</label>
                                    <input type="text" name="txtFirstName"  class="form-control" id="inputFirstName" placeholder="First Name">
                                </div>
                                <div class="form-group">
                                    <label for="lastName">Last Name</label>
                                    <input type="text" name="txtLastName" class="form-control" id="inputLastName" placeholder="First Name">
                                </div>
                                <div class="form-group">
                                    <label for="emailAddress">Email address</label>
                                    <input type="email" name="txtEmail" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
                                    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                                </div>
                                <div class="form-group">
                                    <label for="password">Password</label>
                                    <input type="password" name="txtPassword" class="form-control" id="exampleInputPassword1" placeholder="Password">
                                </div>
                                <div class="form-group">
                                    <label for="Dob">Date of birth:</label>
                                    <input type="date" name="DoB" class="form-control" id="inputDOB" placeholder="First Name">
                                </div>
                                <div class="form-group">
                                    <label for="Address1">Address1</label>
                                    <input type="text" name="txtAddress1" class="form-control" id="inputAddress1" placeholder="First Name">
                                </div>
                                <div class="form-group">
                                    <label for="Address2">Address2</label>
                                    <input type="text" name="txtAddress2" class="form-control" id="inputAddress2" placeholder="First Name">
                                </div>
                                <div class="form-group">
                                    <label for="postcode">Postcode</label>
                                    <input type="text" name="txtPostcode" class="form-control" id="inputPostcode" placeholder="First Name">
                                </div>
                                <input type="submit" value="Submit" name="taxSubmit"  />
                            </form>

                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>

                </div>
            </div>

        </div>

    </body>
</html>