无法在phpmyadmin中的定制表中插入数据

时间:2019-12-29 作者:Arvin Bahilango

我创建了一个dbdelta表,但似乎无法插入任何数据这是我的代码请帮助

function lapizzeria_save_reservation() {
  global $wpdb;
  if(isset($_POST[\'reservation\']) && $_POST[\'hidden\'] =="1")  {
    $name =$_POST[\'name\'];
    $date =$_POST[\'date\'];
    $email =$_POST[\'email\'];
    $phone =$_POST[\'phone\'];
    $message =$_POST[\'message\'];

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

    $data = array(
      \'name\' => $name,
      \'date\' => $date,
      \'email\' => $email,
      \'phone\' => $phone,
      \'message\' => $message
    );

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

  }
}

add_action(\'init\', \'lapizzeria_save_reservation\');
这是html

<form class="reservation-form" method="post">
                <h2>Make a Reservation</h2>
                <div class="field">
                  <input type="text" name="name" placeholder="Name" required>
                </div>
                <div class="field">
                  <input type="datetime-local" name="date" placeholder="Date" required>
                </div>
                <div class="field">
                  <input type="text" name="email" placeholder="E-mail">
                </div>
                <div class="field">
                  <input type="tel" name="phone" placeholder="Phone Number" required>
                </div>
                <div class="field">
                  <textarea name="message" placeholder="Message" requires></textarea>
                </div>
                <input type="submit" name="reservation" class="button" value="Send">
                <input type="hidden" name="hidden" value="1">
            </form>

2 个回复
SO网友:WPTricksDK

我强烈建议为此使用AJAX。

使用“init”向数据库插入一些数据并不是一个好的解决方案。

关于如何使用AJAX,这里有一个很好的指南:https://webprogramo.com/how-to-create-an-ajax-form-in-wordpress/1156/

SO网友:Mradul Sharma
    <?php 
    function lapizzeria_save_reservation() {
    if(isset($_POST[\'reservation\']) && $_POST[\'hidden\'] =="1")  {
    global $wpdb;
        $sql = $wpdb->prepare("INSERT INTO reservation (name, date, email, phone, message)
        VALUES (\'".$_POST["name"]."\',\'".$_POST["date"]."\',\'".$_POST["email"]."\',\'".$_POST["phone"]."\',\'".$_POST["message"]."\')");
        $wpdb->query($sql);
        $name =$_POST[\'name\'];
        $date =$_POST[\'date\'];
        $email =$_POST[\'email\'];
        $phone =$_POST[\'phone\'];
        $message =$_POST[\'message\'];
    function insertuser( $name, $date, $email, $phone, $message){
        global $wpdb;
        $wpdb = $wpdb->prefix . \'reservations\';
    $name = $_POST[\'name\'];
        $date = $_POST[\'date\'];
        $email = $_POST[\'email\'];
    $phone = $_POST[\'phone\'];
    $message = $_POST[\'message\'];
        $format = array(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');
        $success = $wpdb->insert($reservations, array(\'name\' => $name, \'date\' => $date, \'email\'=> $email, \'phone\'=>$phone, \'message\'=>$message ));
    }
    if($success) {
            echo \'Inserted successfully\';
        } else {
            echo \'not\';
        }
        }

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

相关推荐

class-wp-hook.php on line 288

有人能帮我解决这个错误吗?我的网站不工作,甚至wp管理页面。我收到了这个错误消息。我试图禁用所有插件,但仍然是同一个错误?警告:call\\u user\\u func\\u array()要求参数1为有效回调,在/home//public\\u html/wp includes/class wp hook中找不到函数“wp\\u pre\\u kses\\u block\\u attributes”,或函数名无效。php在线288警告:call\\u user\\u func\\u array()要求参