php - 数据在 MySQL 数据库中保存两次。不知道我做错了什么?

标签 php mysql wordpress forms

我正在开发我网站上的一项功能。我使用 OSM 和 Leaflet 实现了一个交互式 map ,我希望用户向该 map 添加位置报告。

尽管如此,我遇到的第一个问题是当用户添加位置报告时发送到网站的数据 MySQL 数据库被注册了两次,这是不应该的。

我恳请您查看我的代码并搜索任何会重复我的数据的东西。

function map_location_report_form()
{

    if( current_user_can( 'create_users' )) 
    {

        global $wpdb;

        $this_page  =   $_SERVER['REQUEST_URI'];
        $page       =   $_POST['page'];

        if ( $page == NULL ) 
        {
            echo '<form method="post" action="' . $this_page .'">

                    <div class="formfield-report" id="formfield-report-firstname">
                        <label for="first_name" id="first_name">Navn: </label>
                        <input type="text" name="first_name" id="first_name" />
                    </div>

                    <div class="formfield-report" id="formfield-report-lastname">
                        <label for="last_name" id="last_name">Efternavn: </label>
                        <input type="text" name="last_name" id="last_name" />
                    </div>

                    <div class="formfield-report" id="formfield-report-locationtype">
                        <label for="location_type" id="location_type">Rapport type: </label>
                        <select name="location_type" />
                            <option value="sigtmelding" selected>Sigtmelding</option>
                            <option value="fangstrapport">Fangstrapport</option>
                            <option value="jagtomraade">Jagtområde</option>
                        </select>
                    </div>

                    <div class="formfield-report" id="formfield-report-latitude">
                        <label for="location_latitude" id="location_latitude">Breddegrad: </label>
                        <input type="text" name="location_latitude" id="location_latitude" />
                    </div>

                    <div class="formfield-report" id="formfield-report-longitude">
                        <label for="location_longitude" id="location_longitude">Længdegrad: </label>
                        <input type="text" name="location_longitude" id="location_longitude" />
                    </div>

                    <input type="hidden" value="1" name="page" />

                    <div id="formfield-report-button">
                        <input class="btn btn-default submit-form-button" type="Submit" />
                    </div>

            </form>';               
        }
        elseif ( $page == 1 ) 
        {
            $first_name             =   $_POST['first_name'];
            $last_name              =   $_POST['last_name'];    
            $location_type          =   $_POST['location_type'];
            $location_latitude      =   $_POST['location_latitude'];
            $location_longitude     =   $_POST['location_longitude'];

            $page_one_inputs =  array
            (
                'first_name'            => $first_name,
                'last_name'             => $last_name,
                'location_type'         => $location_type,
                'location_latitude'     => $location_latitude,
                'location_longitude'    => $location_longitude,
                'page'                  => $page
            );

            $page_one_table = 'maplocationreports';

            $insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);

            echo '<h3>Mange tak for dit bidrag!</h3>';
            echo '<p>Der er sat stor pris på at du har taget dig tid til at registrere et punkt på kortet!</p>';
        }

    }
    else 
    {
        echo    '<p>Desværre er denne service under udvikling.</p>
                 <p>Mange tak for din interesse.</p>';
    }   
};

add_shortcode('map_location_report','map_location_report_form');

如果需要更多信息来了解正在发生的事情,请告诉我。

提前谢谢你。

最好的问候,

最佳答案

检查您的数据库模式,这可能是数据库模式错误的原因,例如不正确的主键/外键或某些字段丢失。

关于php - 数据在 MySQL 数据库中保存两次。不知道我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636244/

相关文章:

php - 在 Codeigniter 中更新用户详细信息

php - 我无法在函数中获取配置变量值作为参数

php - 将逗号分隔的 mysql 标签与数据库值相匹配 (WP - PHP)

javascript - 使用 PHP 变量作为 jQuery 中的变量

php - Paypal 单笔支付 - 'Json request malformed'

php - 需要设置Braintree\Configuration::merchantId(或者需要将accessToken传给Braintree\Gateway)

mysql - 在bash脚本中输出MySQL进程

mysql - 用于 session 的 InnoDB 或 MyISAM?

php - 在交易过程中重新加载页面

css - 有没有一种工具可以在浏览器中识别出确切的 CSS 类?