【问题标题】:Data saved twice in MySQL database. Don't know what I do wrong?数据在 MySQL 数据库中保存了两次。不知道我做错了什么?
【发布时间】:2015-06-20 14:07:32
【问题描述】:

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

虽然,我遇到的第一个问题是当用户添加位置报告时,发送到网站的数据 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');

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

提前谢谢你。

最好的问候,

【问题讨论】:

  • 一个想法;你能确认$page 实际上是NULL 而不仅仅是一个空白值吗?另外,您实际上是如何调用此表单将数据输入数据库的?是否循环运行?
  • 在你的`$wpdb->insert()`之后可能值得尝试exit(),只是为了看看它是否是插入调用本身正在复制行,或者这是否实际上是快速连续叫了两次。我认为您在数据库层中没有触发器或任何花哨的东西?
  • 谁在调用你的函数? map_location_report_form()?这对我来说很奇怪,在函数名称之后看到这一行 current_user_can( 'create_users' )
  • Nomistic - 我正在使用 [map_location_report] 短代码调用表单。
  • 仅供参考 - 上面的代码已添加到我在 WordPress 中的 functions.php 文件中。

标签: php mysql wordpress forms


【解决方案1】:

检查您的数据库架构,这可能是数据库架构错误的原因,例如不正确的主键/外键或缺少某些字段。

【讨论】:

  • 我创建了我的数据库架构如下: CREATE TABLE maplocationreports (id INT( 7 ) NOT NULL AUTO_INCREMENT, first_name VARCHAR( 50 ) NOT NULL, last_name VARCHAR( 50 ) NOT NULL, report_type VARCHAR( 20 ) NOT NULL, location_latitude VARCHAR(12) NOT NULL, location_longitude VARCHAR(12) NOT NULL, page INT(1) NOT NULL, timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) 好像有一些错误的设置。我看不出那个应该是错的。
  • @KimAndreasen - 报告类型?也许代码中缺少...?
  • @Hassen Murtaza - 你是对的。虽然,当我纠正这个错误时,它并没有解决我的问题。还有其他想法吗?
  • @KimAndreasen - 如果 ID 设置为主键,请检查您的主键。你最好再做一张桌子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 2018-10-04
  • 1970-01-01
  • 2017-07-16
  • 2021-04-13
  • 1970-01-01
相关资源
最近更新 更多