我正在尝试编写允许用户向我的数据库添加免费公共厕所的代码。这需要将数据添加到数据库中的3个单独的表中:自治市镇,位置和房间。我正在使用insert_id将查询链接到每个父/子表。
在编写我的代码时,该表单仅在页面上重新加载,而没有任何内容添加到数据库中。是因为我的某些表单输入类型是一个下拉选择菜单和几个复选框(其中一个是数组)引起的问题吗?这是我的第一学期学习php和MySQL,因此,我希望提供有益的指导或其他答案。谢谢!
我已经在该站点上查看了几个答案,但是还没有找到包含我所有输入类型的表单。
#Add a toilet to the database
if (isset($_POST['submit'])) { //check if the form has been submitted
if ((empty($_POST['name'])) || (empty($_POST['location'])) || (empty($_POST['hours'])) || (empty($_POST['boro_id'])) ||
(empty($_POST['boro_name'])) || (empty($_POST['gender'])) || (empty($_POST['handicap_access'])) ||
(empty($_POST['change_table'])) || (empty($_POST['stalls'])) ) {
$message = '
Please fill out all of the form fields!
';} else {
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
$name = sanitizeMySQL($conn, $_POST['name']);
$location = sanitizeMySQL($conn, $_POST['location']);
$hours = sanitizeMySQL($conn, $_POST['hours']);
$boro_id = sanitizeMySQL($conn, $_POST['boro_id']);
$boro_name = sanitizeMySQL($conn, $_POST['boro_name']);
$gender = sanitizeMySQL($conn, $_POST['gender']);
$h_a = sanitizeMySQL($conn, $_POST['handicap_access']);
$c_t = sanitizeMySQL($conn, $_POST['change_table']);
$stalls = sanitizeMySQL($conn, $_POST['stalls']);
$query = 'INSERT INTO `borough` VALUES(NULL, \"$boro_name\")';
$result = $conn->query($query);
$insertID = $conn->insert_id;
$query = 'INSERT INTO `location` VALUES(NULL, \"$name\", \"$location\", \"$hours\", NULL, \"$insertID\" )';
$result = $conn->query($query);
$insertID = $conn->insert_id;
$query = 'INSERT INTO `rooms` VALUES(NULL, \"$insertID\", \"$gender\", \"$h_a\", \"$c_t\", \"$stalls\" )';
$result = $conn->query($query);
我希望表单提交到我的数据库,并告诉用户他们成功了。