Php 在MySQL数据库中保存了两次数据。Don';我不知道我做错了什么?

Php 在MySQL数据库中保存了两次数据。Don';我不知道我做错了什么?,php,mysql,wordpress,forms,Php,Mysql,Wordpress,Forms,我正在我的网站上开发一个功能。我使用OSM和传单实现了一个交互式地图,我希望用户向该地图添加位置报告 尽管如此,我遇到的第一个问题是当用户添加位置报告时,发送到网站MySQL数据库的数据被注册了两次,这是不应该的 我恳请您查看我的代码,并搜索任何会复制我的数据的东西 function map_location_report_form() { if( current_user_can( 'create_users' )) { global $wpdb;

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

尽管如此,我遇到的第一个问题是当用户添加位置报告时,发送到网站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');
function map\u location\u report\u form()
{
if(当前用户可以(“创建用户”))
{
全球$wpdb;
$this\u page=$\u服务器['REQUEST\u URI'];
$page=$_POST['page'];
如果($page==NULL)
{
回声'
导航:
Efternavn:
融洽关系类型:
标志焊接
方舟港
贾格托姆雷德
布雷德格勒:
Længdegrad:
';               
}
elseif($page==1)
{
$first\u name=$\u POST['first\u name'];
$last_name=$_POST['last_name'];
$location\u type=$\u POST['location\u type'];
$location\u latitude=$\u POST['location\u latitude'];
$location\u longitude=$\u POST['location\u longitude'];
$page\u one\u输入=数组
(
“first\u name”=>$first\u name,
“姓氏”=>$last\u name,
“位置类型”=>$location\u类型,
“位置纬度”=>$location\u纬度,
“位置经度”=>$location\u经度,
“页面”=>$page
);
$page_one_table='maplocationreports';
$insert_page_one=$wpdb->insert($page_one_table,$page_one_inputs);
回音“为bidrag的Mange-tak!”;
回音“在登记处和登记处之前,在数据挖掘处保存数据!”;
}
}
其他的
{
在乌德维克林领导下的Desværre denne服务

为din利益管理德。

'; } }; 添加快捷码(“地图位置报告”、“地图位置报告表”);
如果需要更多信息来了解正在发生的事情,请告诉我

先谢谢你

致以最良好的祝愿


Kim

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

一个想法;您能否确认
$page
实际上是
NULL
,而不仅仅是一个空值?另外,您实际上是如何调用此表单将数据输入数据库的?它是通过循环运行的吗?在“$wpdb->insert()”之后尝试一次
exit()
,只是想看看insert调用本身是否在复制行,或者实际上是否在快速连续调用两次。我想您在数据库层中没有触发器或任何花哨的东西?谁在调用您的函数<代码>地图位置报告表单()?这对我来说很奇怪,在函数名之后看到这一行
current_user_can('create_users')
。Nomistic-我正在使用[map_location_report]shortcode.FYI调用表单-上面的代码被添加到WordPress中的functions.php文件中。我创建了我的数据库模式如下:create TABLE maplocationreports(id INT(7)NOT NULL自动递增,名字VARCHAR(50)NOT NULL,姓氏VARCHAR(50)NOT NULL,报告类型VARCHAR(20)NOT NULL,位置纬度VARCHAR(12)NOT NULL,位置经度VARCHAR(12)NOT NULL,页面INT(1)NOT NULL,时间戳时间戳NOT NULL默认当前时间戳,主键(id))它似乎有一些错误的设置。“我看不出这个会有什么问题。”Kimandereasen-报告类型?也许代码中缺少了…?@Hassen Murtaza-你是对的。虽然,当我纠正这个错误时,它并不能解决我的问题。还有其他想法吗?@Kimandereasen-如果ID设置为主键,请检查您的主键。你们最好再整理一下桌子。