php - 重复 - php 代码重复 MySQL 中的行 - 出了什么问题?

标签 php mysql wordpress forms

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

    }
};

创建表 map 位置报告( id INT( 7 ) NOT NULL AUTO_INCRMENT, 名字 VARCHAR( 50 ) NOT NULL, 姓氏 VARCHAR( 50 ) NOT NULL, report_type VARCHAR(20) NOT NULL, location_latitude VARCHAR( 12 ) NOT NULL, location_longitude VARCHAR( 12 ) NOT NULL, 页 INT( 1 ) NOT NULL, 时间戳 TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 主键(id) )

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

提前致谢!

最佳答案

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

举例来说,我想为页面生成一个元描述标记。我可能会将您的帖子内容修剪到一定的长度,但在此之前,我要确保所有短代码都已运行并转换为输出。通过这样做,当您只期望它发生一次时,将行插入数据库的短代码将运行两次。

关于php - 重复 - php 代码重复 MySQL 中的行 - 出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29108633/

相关文章:

php - 使用 PHP 和 MySQL 进行基于订阅的帐户管理,添加时间跨度?

php - 如何获取灯箱图片的标题

PHP Postgres PDO 驱动不支持预处理语句?

python - pandas 将 mysql utf-8 转换为 ascii

javascript - 使 slider 旋转中的形状不可点击

JQUERY:未捕获错误:语法错误,无法识别的表达式::nth-​​child

wordpress - 当用户选择 Paypal 付款方式时,在结帐表单中禁用 WooCommerce 中的账单地址

java - PHP exec() 不接受 java 类路径

php - 使用 while 循环获取图像

mysql - 如何循环遍历结果并删除除最新项目之外的所有项目?