gpt4 book ai didi

php - 简单表单未提交到数据库 PHP MySQL

转载 作者:行者123 更新时间:2023-11-29 08:25:16 25 4
gpt4 key购买 nike

我把头撞在墙上,试图找出为什么我的信息没有提交到我的 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>

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

谢谢

最佳答案

您缺少提交按钮的 name 属性,因此 $_POST 中没有 schcreate 键,并且您的查询无法运行.

您可以通过检查 $_SERVER['REQUEST_METHOD'] 来获取请求类型,从而消除对 name 属性的需求,您还应该使用 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);
}
?>

关于php - 简单表单未提交到数据库 PHP MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18137799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com