treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Wordpress Custom Form Error

  • I'm writing a plugin that posts data to the Wordpress database.

    For the most part I have the basics working. I'm using this to check for duplicate entries.

    $duplicate =  $wpdb->get_results("SELECT * FROM wp_flr_locations WHERE location_name = '{$location_name}'");
    if (isset($_POST['location_name'])) {
      if ($duplicate != '') {
        $wpdb->insert( 'wp_flr_locations', array( 'location_name' => $_POST['location_name'] ) );
      } else {
        echo'No Duplaicates Allowed ';
      }
    } 

    This does prevent duplicate entries from being posted to the database. The problem I'm having is I want an error message to show if it is duplicate. The "else" doesn't seem to work.

    Any Ideas?

  • I would try changing your if statement to something like

    if(!empty($duplicate){

    }

    The point is that WordPress may still be sending data that the if statement as is won't read correctly. From my experience, using empty() works better than using " ";

    My "if" may be backwards, not an expert php programmer here.

  • Thanks I'll give it a try. I'm not a php expert either if you couldn't tell.:)