php - PHP SQL 检查表单输入是否存在于其他表中,然后继续
问题描述
我正在编写一个表单,我希望代码检查参与者想要参加活动的插入“日期”是否与我的活动表中的“日期”匹配。目前(即使表单输入中的“日期”与事件表上的任何日期都不匹配)用户可以注册任何日期而不会收到验证错误。
在 html 表单“register.html”上,我有:
<form action="register.php" method="post">
<div class="form-group">
<label for="date"> Date of attendence </label>
<input type="date" class="form-control" id="date" name="date"/>
</div>
<input type="submit" class="btn btn-primary" />
</form>
在“register.php”我有:
<?php
$date = $_POST['date'];
$conn = new mysqli('localhost','root','','skay');
if($conn->connect_error){
echo "$conn->connect_error";
die("Connection Failed : ". $conn->connect_error);
} else {
$date_check_query = "SELECT COUNT * FROM events WHERE date='$date'";
$number_of_matches = mysqli_query($conn, $date_check_query);
if ($number_of_matches = 0) {
array_push($errors, "No planned events on this day");
} else {
$stmt = $conn->prepare("INSERT INTO registrations(date) values(?)");
$stmt->bind_param("sssssi", $date); //there are other 5 other values aswell
$execval = $stmt->execute();
echo $execval;
echo "Registration successfully...";
$stmt->close();
$conn->close();
}
}
?>
格兹博
解决方案
仔细检查您的代码,尤其是这里
if ($number_of_matches = 0) {
应该
if ($number_of_matches == 0) {
这不在您的问题范围内,但最好考虑 SQL 注入攻击并重新编写以下代码行:
$date_check_query = "SELECT COUNT * FROM events WHERE date='$date'";
推荐阅读
- c - 删除重复数字并使用堆栈打印(C 程序)
- r - 从 r 中的列中删除数字
- java - 在某个字符之后检索子字符串
- android - 如何在 exoplayer 播放列表中添加广告
- reactjs - 创建商店然后将值传递给 mapStateToProps 的问题
- c++ - 如何将运行时类传递给 AfxBeginThread
- mysql - 在 MySQL/MariaDB 中将记录更新/插入到高争用 InnoDB 表中的最佳方法是什么?
- javascript - 设置 html 表单值 - Delphi 10.4 中的 TEdgeBrowser
- django - 如何将 Django Rest APIView 字段暴露给招摇?
- python - 为什么 Pandas Dataframe(和 Python)有一个奇怪的浮点精度十进制值赋值会影响舍入?