首页 > 解决方案 > 将自定义表单中的数据保存在 wordpress 数据库中

问题描述

我创建了一个 PHP 程序,它根据在 HTML 联系表单中输入的用户输入提供自定义输出。HTML 表单和 PHP 输出工作正常。但是,当我尝试将数据保存在 WP 数据库中时,我看不到任何响应。我没有在表单操作中插入任何主题目录位置以使 $region 值能够以 php 形式处理(如果我正在插入主题文件,程序会在提交时重定向到该 URL)。请让我知道如何将用户输入保存在数据库中。请在下面找到代码:

<?php
    $region = $_POST['Region'];
    $Click_here_to_start_your_download = 'https://mydailysugar.info/wp-content/uploads/2019/09/Blood-Glucose-Chart-A4-1.pdf';
    switch ($region) {
        case 'North':
            echo "<a href='$Click_here_to_start_your_download'>Click here to start your download</a>";
            die;
        case 'South':
            echo "<a href='$Click_here_to_start_your_download'>Click here to start your download</a>";
            die;
        case 'East':
            echo "<a href='$Click_here_to_start_your_download'>Click here to start your download</a>";
            die;
        case 'West':
            echo "<a href='$Click_here_to_start_your_download'>Click here to start your download</a>";
            die;
    }
    $name = $_POST['user_name'];
    $email = $_POST['user_email'];
    $phone = $_POST['user_phone'];

    function insertuser($name, $email, $phone) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'mealplanner';
        $wpdb->insert($table_name, array
            ('user_name' => $name, 'user_email' => $email, 'user_phone' => $phone)
        );
    }

    insertuser($name, $email, $phone);
?>

<html>
    <head>
        <title> Meal Planner </title>
    </head>
    <body>
        <form method='POST'>
            <p>Name</p> <input type='text' name='user_name'>
            <p>Email</p> <input type='text' name='user_email'>
            <p>Phone</p> <input type='text' name='user_phone'>
            <p>Dropdown Box</p>
            <select name='Region' size='1'>
                <option value='North'>North
                <option value='South'>South
                <option value='East'>East
                <option value='West'>West
            </select>
            <br/>
            <input type='submit' value='SUBMIT'><input type='reset' value='CLEAR'>
        </form>
    </body>
</html>

标签: phpdatabasewordpress

解决方案


推荐阅读