【问题标题】:Duplication - php code duplicates row in MySQL - What is wrong?重复 - php 代码在 MySQL 中重复行 - 出了什么问题?
【发布时间】:2015-05-20 10:40:36
【问题描述】:

关于 stockoverflow 的社区您好!

我正在开发一个表单,用于在我的 MySQL 数据库的表中收集简单数据。
我对 php 语言还很陌生,这意味着我很可能误解或忘记了一些东西。

我的 php 表单代码有问题。它在我的 MySQL 数据库中添加了两次相同的行。有人知道代码有什么问题吗?

add_shortcode('map_location_report','map_location_report_form');

function map_location_report_form()
{
    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="report_type " id="report_type ">Rapport type: </label>
                    <select name="report_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'];
        $report_type            =   $_POST['report_type '];
        $location_latitude      =   $_POST['location_latitude'];
        $location_longitude     =   $_POST['location_longitude'];       

        $page_one_table = 'maplocationreports';

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

        $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>';

    }
};

创建表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 ) )

如果您发现任何错误,请告诉我。我将不胜感激!

提前致谢!

【问题讨论】:

  • 代码似乎正确。但是,如果您在页面中多次添加简码,则每次使用简码时代码都会运行。这是我能看到的第一件事,这可能是问题所在。
  • 嗨,Knarf。感谢你的回复。我只在 WordPress 的一个页面中使用了一次简码。我相信重复发生在我的代码中的某个地方,但我没有足够的经验来找到问题。
  • 删除操作字段会改变什么吗?

标签: php mysql wordpress forms


【解决方案1】:

您需要将处理表单的代码移到简码之外。短代码应该用于控制屏幕上显示的内容。通过分离这两个职责,您的问题应该得到解决。仅仅因为内容只显示一次并不意味着您的短代码只使用一次。其他代码可以与之交互。

假设我想为页面生成一个元描述标签。我可能会将您的帖子内容缩减到一定长度,但在此之前,我要确保所有短代码都已运行并转换为它们的输出。通过这样做,您在数据库中插入一行的短代码将运行两次,而您只希望它发生一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    相关资源
    最近更新 更多