场合
我有一份提交表格。使用此表单,用户可以在餐厅预订餐桌。每次提交都会有一些数据通过wp_insert_post
函数发送到数据库。例如user_ip
和datum_nieuwe_reservering
(稍后解释)。我想用它来检查是否允许用户提交新的预订(防止发送垃圾邮件)。我尝试过几件事,但它没有用。请参阅下面的代码。
问题
我怎样才能让它发挥作用?我做错了什么?
CODE
reserving.php
<!-- CODE WHEN POST = SUBMIT, MAKE A NEW RESERVATION.
=========================================== -->
<?php
global $wpdb;
$user_ip_db = getUserIP();
$date = date("d-m-Y H:i:s");
//echo $date;
$check_user_ip = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'user_ip_reservering' AND meta_value = '$user_ip_db'");
$check_time_new = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'datum_nieuwe_reservering'");
print_r($check_time_new);
if ( isset($_POST['submit_reservering']) ) {
if ( (empty($check_user_ip)) && $date > $check_time_new ) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}
?><!-- end CODE FOR MAKING RESERVATION-->
插件文件
//Using this function to send the current day + 1 day to the database
//with a hidden text field named `datum_nieuwe_reservering`(show below)
//and in the end check if current date >= `datum_nieuwe_reservering`
<?php
function getDateTime(){
date_default_timezone_set('Europe/Amsterdam');
$next_day = time() + (1 * 24 * 60 * 60);
$date_now = date('d-m-Y H:i:s');
$date_tomorrow = date('d-m-Y H:i:s', $next_day);
echo $date_tomorrow;
}
?>
datum_nieuwe_reservering
<input type='hidden' value='<?php echo getDateTime(); ?>' name='datum_nieuwe_reservering'>
注意:提交表单仅适用于if (empty($check_user_ip)) { };
代码。所以我也很难在if语句中添加日期。
我希望有人能帮助我朝着正确的方向前进! 提前谢谢。
答案 0 :(得分:0)
您是否尝试过使用括号?
if ( isset($_POST['submit_reservering']) ) {
if ( (empty($check_user_ip)) && ($date > $check_time_new) ) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}
答案 1 :(得分:0)
if (isset($_POST['submit_reservering']) === true) {
$reserved = false;
if (empty($check_user_ip) === true) {
$test = end($check_time_new);
if(date > $test->meta_value) {
create_reservering();
echo "Your reservation has been send";
} else {
echo 'You have already reserved';
}
}
}