Data saved twice in MySQL database. Don't know

2019-08-06 07:39发布

问题:

I am working on a feature on my website. I implemented a interactive map using OSM and Leaflet and I would like users to add location reports to this map.

Although, the first issue i ran into is when the users are adding a location report the data that is being send to the websites MySQL database is registered twice which it should not.

I kindly ask you to take a look at my code and search for any thing that would duplicate my data.

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

Let me know if more information is necessary to understand what is going on.

Thank you in advance.

Best regards,

Kim

回答1:

Check your database schema, it can be cause of wrong database schema like incorrect primary key/foreign key or some field missing.