表单提交不适用于PHP

时间:2018-07-14 09:04:09

标签: php mysql backend

当我单击Submit(提交)按钮时,没有任何反应,它不会重定向,也不会将html形式的数据发布到数据库中。

这是我的日期,时间和位置代码:

<?php
session_start();
$date1="";
$time1="";
$location="";
$db=mysqli_connect("localhost","root","","registration");
if(isset($_POST['submit'])) {
    $date1 = mysqli_real_escape_string($db, $_POST['date1']);
    $time1 = mysqli_real_escape_string($db, $_POST['time1']);
    $location = mysqli_real_escape_string($db, $_POST['location']);


    if (!empty($date)) {
        $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')";
        mysqli_query($db, $query);
        header('location:tariff.php');
    }
}


?>

在此HTML示例中,按钮名称为Submit,单击该按钮时,PHP设置为True,应执行代码,但是存在一些错误。

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


    <style>
        .form-inline{
            text-align: center;
            position: absolute;
            top: 100px;
            left: 500px;

        }
        .btn{
            position: absolute;
            top: auto;
            left: 500px;
        }
    </style>
</head>
<body>
<div class="container"
     <div class="panel panel-default"
<form class="form-inline" method="post" action="bookcar.php">
    <div class="panel-body">
    <p><label> Pick a date:</label><input class="form-control" type="date" name="date1" placeholder="Date"></p>
    </div>
    <div class="panel-body">
    <p><label> Pick a Time:</label><input class="form-control" type="time" name="time1" placeholder="Time"></p>
    </div>
    <div class="panel-body">
        <p><label> Pick a Location:</label><input class="form-control" type="text" name="location" placeholder="Location"></p>
    </div>
    <input type="submit" name="submit" value="submit" class="btn btn-lg btn-primary">

</form>
</div>
</div>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

 session_start();

 $db=mysqli_connect("localhost","root","","registration");

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

     $date1 = mysqli_real_escape_string($db, $_POST['date1']);

 }

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

     $time1 = mysqli_real_escape_string($db, $_POST['time1']);

 }

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

     $location = mysqli_real_escape_string($db, $_POST['location']);

 }

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

     if (!empty($date1)){

         $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')";
         mysqli_query($db, $query);
         header('location:tariff.php');
         exit(); // Quit current page and go to tariff.php

     }
 } 

答案 1 :(得分:0)

您的代码中有小错误。用下面的代码替换您的代码。

if (!empty($date1)) {
        $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')";
        mysqli_query($db, $query);
        header('location:tariff.php');
    }

您尚未在代码中定义$ date变量,并且在没有条件的情况下使用$ date变量检查其是否为空。

答案 2 :(得分:0)

首先请确保bookcar.php与html文件位于同一位置
注意:它们应该位于同一文件夹中。
将您的bookcar.php文件更改为此

<?php
session_start();
$date1="";
$time1="";
$location="";
$db=mysqli_connect("localhost","root","","registration");
if(isset($_POST['submit'])) {
    $date1 = mysqli_real_escape_string($db, $_POST['date1']);
    $time1 = mysqli_real_escape_string($db, $_POST['time1']);
    $location = mysqli_real_escape_string($db, $_POST['location']);


    if (!empty($date1)) {
        $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')";
        mysqli_query($db, $query);
        header('location:tariff.php');
    }
}
?>