我正在编写一个表单,我希望代码检查插入的参与者想要参加活动的“日期”是否与我的活动表中的“日期”匹配。目前(即使表单输入中的“日期”与事件表中的任何日期都不匹配)用户可以注册任何日期而不会收到验证错误。
在 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();
}
}
?>
格茨博
如果分配alias
到返回的值count(*)
-样count(*) as 'aliasname'
,你可以把它看成你会在记录的定期专栏。
使用 aprepared statement
来输入数据很好,但是如果直接在select
查询中使用潜在的恶意数据,那么好的工作就会被取消——无论您在何处输入用户输入,都需要采取额外的预防措施,因此在所有此类情况下都使用准备好的语句。
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['date'] ) ){
$date = $_POST['date'];
$conn = new mysqli('localhost','root','','skay');
$sql = "SELECT COUNT(*) as `total` FROM `events` WHERE `date`=?";
$stmt=$conn->prepare($sql);
$stmt->bind_param('s',$date);
$stmt->execute();
$stmt->bind_result($total);
$stmt->fetch();
$stmt->close();
if( $total==0 ){
$errors[]="No planned events on this day";
}else{
$sql='insert into `registrations`( `date` ) values(?)';
$stmt=$conn->prepare($sql);
$stmt->bind_param('s',$date);
$stmt->execute();
$stmt->close();
$conn->close();
echo "Registration successfully...";
}
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句