Form doesnt save to database

时间:2020-03-31 作者:Faruk rıza

这是我第一次尝试wordpress。我将一些名称(如“name”、“e-mail”等)更改为“name1”、“email1”等。因为如果不进行这些更改,提交按钮指的是404页。

当我检查数据库时,它上没有任何注册

我不知道什么是问题的特殊代码,所以我给出了我的所有代码。如果你知道足够的信息,也许你可以很容易地解决这个问题。但若你们并没有足够的信息,你们可能会对这个问题给出否定的观点,然后离开我的问题。或者,你可以结束我的问题,写下“给我们特定的代码/问题”。我再次写信,我的问题或提问方式没有问题,这完全是你的信息。

<!-- data mata -->
    <div class="reservation-info">
        <form class="reservation-form" method="post">
            <h2>Make a reservation</h2>
            <div class="field">
                <input type="text" name="name1" placeholder="Name" required>
            </div>
            <div class="field">
                <input type="datetime-local" name="date1" placeholder="Date" step="300" required>
            </div>
            <div class="field">
                <input type="text" name="email1" placeholder="E-Mail">
            </div>
            <div class="field">
                <input type="tel" name="phone1" placeholder="Phone Number" required>
            </div>

            <div class="field">
                <textarea name="message1" placeholder="Message" requires></textarea>
            </div>

            <div class="g-recaptcha" data-sitekey="6LeKTjMUAAAAAJuZI0qqIBRp92slJoG4SESblWHw"></div>

            <input type="submit" name="reservation1" class="button" value="Send">

            <input type="hidden" name="hidden" value="1">
        </form>
    </div>
<小时>
<?php 
    function lapizzeria_database(){
        global $wpdb;

        global $lapizzeria_db_version;
        $lapizzeria_db_version = "1.0";

        $table = $wpdb->prefix . \'reservations1\';

        $charset_collate = $wpdb->get_charset_collate();

        // SQL Statement

        $sql = "CREATE TABLE $table ( 
                id mediumint(9) NOT NULL AUTO_INCREMENT, 
                name1 varchar(50) NOT NULL,
                date1 datetime NOT NULL,
                email1 varchar(50) DEFAULT \'\' NOT NULL,
                phone1 varchar(10) NOT NULL,
                message1 longtext NOT NULL,
                PRIMARY KEY (id)
        ) $charset_collate; ";

        require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
        dbDelta($sql);
    }

    add_action(\'after_setup_theme\', \'lapizzeria_database\');
<小时>
function lapizzeria_save_reservation() {

    if(isset($_POST[\'reservation1\']) && $_POST[\'hidden\'] == "1") {
              // read the value from recaptcha response
              $captcha = $_POST[\'g-recaptcha-response\'];

              // Send the values to the server
              $fields = array(
                  \'secret\' => \'6LeKTjMUAAAAAFeaj6Hq941AFvASw9sBJjeiCDyB\',
                  \'response\' => $captcha,
                  \'remoteip\' => $_SERVER[\'REMOTE_ADDR\']
              );

              // Start the request to the server

              $ch = curl_init(\'https://www.google.com/recaptcha/api/siteverify\');

              // configure the request
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch, CURLOPT_TIMEOUT, 15);

              // Send the encode values in the URL 
              curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

              // Read the return value
              $response = json_decode(curl_exec($ch));
              if($response->success) {
                          global $wpdb;
                          $name = sanitize_text_field( $_POST[\'name1\'] ) ;
                          $date = sanitize_text_field( $_POST[\'date1\'] ) ;
                          $email = sanitize_email( $_POST[\'email1\'] );
                          $phone = sanitize_text_field( $_POST[\'phone1\'] ) ;
                          $message = sanitize_text_field( $_POST[\'message1\'] ) ;

                          $table = $wpdb->prefix . \'reservations1\';

                          $data = array(
                            \'name1\' => $name1,
                            \'date1\' => $date1,
                            \'email1\' => $email1,
                            \'phone1\' => $phone1,
                            \'message1\' => $message1
                          );

                          $format = array(
                            \'%s\',
                            \'%s\',
                            \'%s\',
                            \'%s\',
                            \'%s\'
                          );
                          $wpdb->insert($table, $data, $format );

                          $url = get_page_by_title(\'Thanks for your reservation!\');
                          wp_redirect( get_permalink($url) );
                          exit();
              }


    }
}

add_action(\'init\', \'lapizzeria_save_reservation\');

 ?>
<小时>
function lapizzeria_reservations() { ?>
  <div class="wrap">
      <h1>Reservations</h1>
      <table class="wp-list-table widefat striped">
          <thead>
              <tr>
                  <th class="manage-column">ID</th>
                  <th class="manage-column">Name</th>
                  <th class="manage-column">Date of Reservation</th>
                  <th class="manage-column">Email</th>
                  <th class="manage-column">Phone Number</th>
                  <th class="manage-column">Message</th>
                  <th class="manage-column">Delete</th>
              </tr>
          </thead>

          <tbody>
              <?php 
                global $wpdb;
                $table = $wpdb->prefix . \'reservations1\';
                $reservations = $wpdb->get_results("SELECT * FROM $table", ARRAY_A); 
                foreach($reservations as $reservation): ?>
                    <tr>
                        <td><?php echo $reservation[\'id\']; ?></td>
                        <td><?php echo $reservation[\'name1\']; ?></td>
                        <td><?php echo $reservation[\'date1\']; ?></td>
                        <td><?php echo $reservation[\'email1\']; ?></td>
                        <td><?php echo $reservation[\'phone1\']; ?></td>
                        <td><?php echo $reservation[\'message1\']; ?></td>
                        <td>
                            <a href="#" class="remove_reservation" data-reservation="<?php echo $reservation1[\'id\']; ?>">Remove</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
          </tbody>
      </table>
  </div>



<?php }


 ?>

2 个回复
最合适的回答,由SO网友:Faruk rıza 整理而成

为什么@Sufyan Sheikh在他的回答中更改了密码我不知道,但他的信息是不真实的。真正的代码有问题。

代码没有问题。问题是电话号码输入值只能是19个字符。所以,您必须删除电话号码中的空格,并检查它是否为10个字符。这很简单,但很有效。

SO网友:Sufyan Sheikh

您的代码中有一个小错误。Use same variables 在里面$data array 以及存储表单数据。将代码更改为:

$data = array(
    \'name1\' => $name,
    \'date1\' => $date,
    \'email1\' => $email,
    \'phone1\' => $phone,
    \'message1\' => $message
);
您已使用$name 在一个地方和$name1 在下面的另一个地方。

相关推荐

Gravity Forms - RMA Count

我是Wordpress的新手,所以想知道是否有人可以帮忙?我正在通过重力表单创建一个返回表单。然而,在每个表单的顶部,我想显示一个退货表单编号,例如RMA001、RMA002等。我想这可能可以使用一些编码来完成,但不知道如何编码。请你让我知道如何做到这一点,并解释我需要在哪里添加代码。如果您能掌握简单的英语,我们将不胜感激!非常感谢!本