WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using $wpdb->get_results causes page to die (3 posts)

  1. strictlyjc
    Member
    Posted 1 year ago #

    Hello,

    I am working on a registration form and I want to restrict each email to be registered just once. It's working perfectly on my localhost but on the remote site, the registration page dies when it is loaded.

    The initial code, which allowed multiple registration, worked both locally and remotely:

    if (!empty($_POST['email'])) {
    	$email = $_POST['email'];
    } else {
    	$error .= 'Please give us your email so we can contact you if we need to<br />';
    }

    But it acts up remotely once I add the database call. Here's the other code:

    if (!empty($_POST['email'])) {
    	global $wpdb;
    	/* wpdb class should not be called directly.global $wpdb variable is an instantiation of the class already set up to talk to the WordPress database */
    	$result = $wpdb->get_results( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'email'" );
    	foreach ( $result as $row ){
    		if(stristr($row->meta_value, $_POST['email'])){
    			goto a;
    		} else {
    			$email = $_POST['email'];
    		}
    	}
    	a:
    	$error .= 'The email address: <span>' . $_POST['email'] . '</span> has already been registered.<br />';
    } else {
    	$error .= 'Please give us your email so we can contact you if we need to<br />';
    }
  2. bcworkz
    Member
    Posted 1 year ago #

    'goto' is a relatively recent operator for php, your hosted version is probably not running a very recent version. Restructure your loop so goto is not required and all should be OK.

    FWIW, many programmers feel goto is a hack construct and does not belong in any language. Admittedly, it can be quite handy sometimes, I personally avoid using it unless I'm really stuck.

  3. strictlyjc
    Member
    Posted 1 year ago #

    Thanks bcworkz! I realised I really didn't need the 'goto'. Once I took it out and inserted the error directly it worked perfectly!

    Cheers!

Topic Closed

This topic has been closed to new replies.

About this Topic