php - 表单数据未在 MySql 数据库中提交
问题描述
我在MySql数据库中提交数据时遇到问题,我的代码没有错误,但是当我检查数据库时,似乎表中没有提交数据
这是我的 php 代码
if(isset($_GET['date'])){
$date = $_GET['date'];
}
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$timeslot = $_POST['timeslot'];
$mysqli = new mysqli('localhost', 'root', '*******', 'odpas');
$stmt = $mysqli->prepare("INSERT INTO 'bookings' ('name', 'email', 'date', 'timeslot') VALUES (?,?,?,?)");
$stmt->bind_param('ssss', $name, $email, $date, $timeslot);
$stmt->execute();
$msg = "<div>Booking Successfully</div>";
$stmt->close();
$mysqli->close();
}
这是表格
<form action="" method="post">
<h4>Booking: <p id="slot"></p>
</h4>
<label> Name </label>
<input required type="text" name="name">
<label> Email </label>
<input required type="email" name="email">
<label> TIMESLOT </label>
<input required type="text" readonly name="timeslot" id="timeslot">
<button type="submit">
SUBMIT
</button>
</form>
代码没有分开,当我单击“提交”按钮时,它看起来正在工作,但是当我检查数据库时没有提交数据。
非常感谢您的帮助!
解决方案
您忘记提供名称提交按钮并检查isset($_POST['submit'])这就是如果条件未运行内部代码的原因。
if(isset($_GET['date'])){
$date = $_GET['date'];
}
if(isset($_POST['submit'])){
//print_r($_POST);
$name = $_POST['name'];
$email = $_POST['email'];
$timeslot = $_POST['timeslot'];
$mysqli = new mysqli('localhost', 'root', '*******', 'odpas');
$stmt = $mysqli->prepare("INSERT INTO 'bookings' ('name', 'email', 'date', 'timeslot') VALUES (?,?,?,?)");
$stmt->bind_param('ssss', $name, $email, $date, $timeslot);
$stmt->execute();
$msg = "<div>Booking Successfully</div>";
$stmt->close();
$mysqli->close();
}
?>
<form action="" method="post">
<h4>Booking: <p id="slot"></p>
</h4>
<label> Name </label>
<input required type="text" name="name">
<label> Email </label>
<input required type="email" name="email">
<label> TIMESLOT </label>
<input required type="text" readonly name="timeslot" id="timeslot">
<button type="submit" name="submit">
SUBMIT
</button>
</form>
推荐阅读
- python-3.x - 范围内的 Python 列表
- python - 如何在多重继承中使用`super`?
- postgresql - PGADMIN 4 Linux 二进制路径
- sql - 进入多行的 Sql 输出转换为单行
- sql - 检索具有匹配模式的字符串和带有连字符 sql server 2008 的内容
- bash - 当 || 右侧的命令时,shell 失败 不存在
- r - Google Places API 不包括永久关闭的位置?
- vue.js - 使用 VSCode 在 Vue.js 中调试 Mocha 单元测试的配置
- angular - 如何在Angular中单击按钮时将FormControl动态添加到FormArray?
- spring-boot - 将 kie-server 集成到 Spring Boot 应用程序中