Duplication - php code duplicates row in MySQL - What is wrong?

983

Hello community on stockoverflow!

I am developing a form for collecting simple data within a table in my MySQL database.
I am fairly new at the php-language which means that i have mostly likely misunderstood or forgot something.

I am having an issues with my php form code. It adds the same row twice within my MySQL database. Does anybody know what is wrong with the code?

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 id="formfield-report-firstname">
          <label for="first_name" id="first_name">Navn: </label>
          <input type="text" name="first_name" id="first_name" />
        </div>

        <div id="formfield-report-lastname">
          <label for="last_name" id="last_name">Efternavn: </label>
          <input type="text" name="last_name" id="last_name" />
        </div>

        <div 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 id="formfield-report-latitude">
          <label for="location_latitude" id="location_latitude">Breddegrad: </label>
          <input type="text" name="location_latitude" id="location_latitude" />
        </div>

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

  }
};

CREATE TABLEmaplocationreports( 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 ) )

Please let me know if you see any mistakes. I would much appreciate it!

Thanks in advance!

268

Answer

Solution:

You need to move the code for processing the form outside of the shortcode. The shortcode should be used to control what is displayed on the screen. By separating these two responsibilities your issue should be resolved. Just because the content is only displayed once doesn't mean your shortcode is only used once. Other code can interact with it.

Let's say for example that I want to generate a meta description tag for the page. I might trim your post content down to a certain length but before I do that I'm going to want to make sure all shortcodes have been run and converted into their output. By doing so your shortcode that inserts a row into the DB will be run twice when you're only expecting it to happen once.

People are also looking for solutions to the problem: javascript - Onmousedown / OnmouseUp issue with website

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.