使用我的插件将表单数据提交到WordPress现有数据库表

时间:2018-06-01 作者:Timothy Mach

我已经为wordpress创建了一个插件,将表单dtails提交到wordpress数据库。我在cpanel phpmyadmin中创建了一个名为“wp\\u in\\u booking”的表。我试图发送表单数据,但无法通过。这是我的密码。数据不会发送到数据库。请帮忙。非常感谢。

<?php
function in_booking_form(){ 
   ob_start();
    ?>
    <link href="<?php echo plugin_dir_url(__FILE__).\'/files/jquery.datetimepicker.css\'; ?>" rel="stylesheet" type="text/css">
    <script src="<?php echo plugin_dir_url(__FILE__).\'/files/jquery.datetimepicker.js\'; ?>"></script>

    <form class="in-checkout-form" action=""  method="post">
        <div class="in-booking-section">
        <input required type="text" id="date_timepicker_start" class="in-dates" name="check_in" placeholder="check-in">
        // there is other javascript code here

        <input type="hidden" name="total_amount" value="">
        <input type="hidden" name="adults" value="1">
        <input type="hidden" name="children" value="0">
        <input type="hidden" name="infants" value="0">
      <input type="submit" class="in-request-to-book" value="Request to Book" name="submit">
    <?php 
      global $wpdb;

  if(isset($_POST[\'submit\'])){
    // $table = $wpdb->prefix . "wp_in_booking";
    $check_in = $_POST[\'check_in\'];
    $check_out = $_POST[\'check_out\'];
    $total_amount = $_POST[\'total_amount\'];
    $adults  =$_POST[\'adults\'];
    $children = $_POST[\'children\'];
    $infants = $_POST[\'infants\'];

    $check_in_db=date("Y-m-d",strtotime($check_in));
    $check_out_db=date("Y-m-d",strtotime($check_out));

//    

    $result = $wpdb->insert(\'wp_in_booking\', array(
      \'check_in\'=>$check_in_db, 
      \'check_out\'=>$check_out_db, 
      \'total_amout\' =>$total_amount,
      \'adults\'=>$adults,
      \'children\'=>$children,
      \'infants\'=>$infants

    ));
    if($result){
      echo "Booking Successful";
    }else{
      echo "Booking Unsuccessful! Please Contact Support";
      }

    }
   ?>
    </form>

    <?php

    return ob_get_clean();

}
add_shortcode(\'in_booking_form\',\'in_booking_form\');
和我的数据库结构

enter image description here

1 个回复
最合适的回答,由SO网友:Timothy Mach 整理而成

我想我找到了问题所在。数据库中签入和签出的数据类型设置为DATETIME 而不是DATE 然而,代码$check_in_db=date("Y-m-d",strtotime($check_in)); 正在将其转换为日期。我不知道为什么它阻止了其他数据的输入,但在更改后,它现在正在工作。要使用日期时间,请使用$check_in_db=date("Y-m-d H:i:s",strtotime($check_in));

结束