简单的表单没有提交到数据库PHP MySQL

时间:2013-08-08 23:15:41

标签: php mysql select

在墙上试着弄清楚为什么我的信息没有被提交到我的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>

我已经尝试了所有我能想到的东西,看看发生了什么,但我没有收到任何错误。任何人都可以看到我不在这里吗?

由于

2 个答案:

答案 0 :(得分: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); 
    }
?>    

答案 1 :(得分: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>