0

把我的头撞在墙上,试图弄清楚为什么我的信息没有被提交到我的 MySQL 数据库中。所有连接信息都是正确的,我使用 1 个连接脚本,其他一切仍然正常。在我进行测试时,用于提交的 PHP 是非常基本的(即在我正确提交之后会进行卫生和验证)。但是无论出于什么原因,这段代码都不会提交。它与我在其他地方使用的代码相同,因此只有这个有问题。

PHP 代码

<?php       

if(isset($_POST["schcreate"])){

error_reporting(E_ALL);

  $name = "";

  $location = "";

  $daystart = "";

  $shiftcount = "";

  $startdate = "";

 include_once("../php_includes/db_connect.php");

 $name = $_POST['schname'];

 $location = $_POST['schlocation'];

 $daystart = $_POST['schday'];

 $shiftcount = $_POST['schshiftcount'];

 $startdate = $_POST['schstart'];

 $sql = "INSERT INTO schinformations (schname, schlocation, schdaystart, schshiftcount, schstartdate) VALUES ('$name', '$location', '$daystart', '$shiftcount', '$startdate');";

$query = mysqli_query($db_connect, $sql); 

}
?>    

HTML 代码

<form method="post" action="">
<div class="schq">Schedule Name:</div>
<div class="scha"><input type="text" id="schname" name="schname" /></div>
<div class="schq">Location:</div>
<div class="scha"><input type="text" id="schlocation" name="schlocation" /></div>
<div class="schq">Day of the week the schedule starts on:</div>
<div class="scha"><select id="schday" name="schday">
                <option value="default">Select </option>
                <option>Monday</option>
                <option>Tuesday</option>
                <option>Wednesday</option>
                <option>Thursday</option>
                <option>Friday</option>
                <option>Saturday</option>
                <option>Sunday</option>
            </select>
</div>
<div class="schq">Number of Shifts:</div>
<div class="scha"><input type="text" id="schshiftcount" name="schshiftcount" /></div>
<div class="schq">Start Date:</div>
<div class="scha"><input type="text" id="schstart" name="schstart" /></div>
<div class="schq">Next Step:</div>
<div class="scha"><input type="submit" id="schcreate" value="Create Shifts"/></div>
</form>

我已经尝试了我能想到的一切来看看发生了什么,但我没有收到任何错误。谁能看到我不在这里?

谢谢

4

2 回答 2

2

您缺少name提交按钮的属性,因此没有schcreate输入$_POST,您的查询也不会运行。

name您可以通过检查$_SERVER['REQUEST_METHOD']获取请求类型来消除对属性的需求,您也应该isset在每个帖子值中使用,因此您可以像这样重写代码:

<?php       
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        error_reporting(E_ALL);
        include_once("../php_includes/db_connect.php");
        $name = isset($_POST['schname']) ? $_POST['schname'] : '';
        $location = isset($_POST['schlocation']) ? $_POST['schlocation'] : '';
        $daystart = isset($_POST['schday']) ? $_POST['schday'] : '';
        $shiftcount = isset($_POST['schshiftcount']) ? $_POST['schshiftcount'] : '';
        $startdate = isset($_POST['schstart']) ? $_POST['schstart'] : '';

        $sql = "INSERT INTO schinformations (schname, schlocation, schdaystart, schshiftcount, schstartdate) VALUES ('$name', '$location', '$daystart', '$shiftcount', '$startdate');";
        $query = mysqli_query($db_connect, $sql); 
    }
?>    
于 2013-08-08T23:41:06.783 回答
0

干净利落:

<div class="scha"><input type="submit" id="schcreate" value="Create Shifts"/></div>

应该是:

<div class="scha"><input type="submit" id="schcreate" name="schcreate" value="Create Shifts"/></div>
于 2013-08-08T23:54:54.010 回答