Wordpress mysql errors

i am a beginner here

i need a mail subscription thing

So with php and mysql i managed to create this form

   <?php

  /*
  Template name: mail form
  */
 // if using a custom function, you need this
 $Email = $_POST['email-form'];
 $firstn = $_POST['First-name'];


   global $wpdb;
   $table_name = $wpdb->prefix . "new_mail_form";
   $wpdb->insert( $table_name, array( 'email' => $Email, 'name' => $firstn ) )


    ?>

    <form method="post" action="#"  enctype="multipart/form-data" id="Submitform" >

 <fieldset class="email-form"> 
   <label for="email-form">:</label> 
   <input type="text" name="email-form" id="email-form"  placeholder="Enter Your Email here" />

</fieldset>

  <fieldset class="First-name"> 
  <label for="First-name">:</label> 
  <input type="text" name="First-name" id="First-name"  placeholder="Enter Your Name here" />

 </fieldset>
  <  input type="submit" value="Publish Post" tabindex="40" id="submit" name="submit" class="submitbutton" />
  </form>

But the problem is that on every page refresh it adds a entry in my sql

so when someone loads the page it automatically adds blank entry

How can i prevent doing that ?/

Solutions

That's beacuse you're not checking if the form is submitted first. Try this:

if (!empty($_POST)) {
    $Email = $_POST['email-form'];
    $firstn = $_POST['First-name'];

    global $wpdb;
    $table_name = $wpdb -> prefix . "new_mail_form";
    $wpdb -> insert($table_name, array('email' => $Email, 'name' => $firstn));
}
Tags: PHP / Mysql / Wordpress

Similar questions

Finding errors in this step of mysql installation in ubuntu 14.04 "sudo mysql_install_db "
Installing MySQL system tables... 150623 11:01:43 [Note] /usr/sbin/mysqld (mysqld 5.5.43-0ubuntu0.14.04.1) starting as process 24482 ... OK Filling help tables... 150623 11:01:43 [Note] /usr/sbin/mysqld (mysqld 5.5.43-0ubuntu0.14.04.1) starting as process 24488 ... OK To start mysqld at boot time you have to copy support-files/mysql.server to the r...
Server overload: mysql errors
My server is overloaded and crashed.. In error_log I have the following error: When server is overloaded the error apear up to 50 times/second. Do you have any ideea why this is happening?
Error #1064: phpMyAdmin import showing html errors in a MYSQL database - why html?
I'm trying to import a database into phpMyAdmin (I'm using MAMP) and I'm getting the Static Analysis Error report below. I exported the db using phpMyAdmin from a Bluehost server. I have also tried MYSQL40 format, but the same error occurs. I don't understand why there is is html showing here - any clarification would be very helpful. I've trawled ...
If the only thing I use MySQL for is Wordpress, do I need MySQL's timezone tables?
The only thing I have MySQL around for is Wordpress. Some MySQL features, I gather, require you to import the system timezone database into special tables in the mysql database. (Which seems like a silly way to do things, but never mind.) Does Wordpress make use of any of those features? The MySQL global time zone is set to 'SYSTEM', and the server...
where I am going wrong in this MySQL query for wordpress Mysql?
I want to get the list of posts through this query. Can anyone can solve this query?
How to order my MySQL records ? | MySQL - WordPress
I am creating a website, for a client based on WordPress. My client want's in home page to display articles grouped under categories. In example, let's say we have the categories "Cat A", "Cat B" and "Cat C". My client want's to display the categories order in the following way "Cat B", "Cat A" and "Car C", in the home page, and then list 6 post fr...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.