Jump to content

Using explode() and arrays


Imoddedu

Recommended Posts

Hey! I get the error: Notice: Undefined offset: 1whenever I run this code:

  // Filter and Split Email    $splitemail = explode('@', $email);  if(checkdnsrr("$splitemail[1]")) {	 $email = mysql_real_escape_string($email);  }  else {	$error = '2';  }

Shouldn't there be a $splitemail[1]?

Link to comment
Share on other sites

Check if there is a $splitemail[1]. var_dump($splitemail);Removed the double quotation marks around $splitemail[1].

Link to comment
Share on other sites

I'm guessing your passing explode a String; $email. Explode expects an array and will treat a string as an array of characters ie:

me@gmail.com becomes->"m","e","@","g","m","a","i","l",".","c","o","m"

Therefore when you say $splitemail[1] there isn't any members in this array so you have an undefined offset as $email was never exploded.

Link to comment
Share on other sites

I think that might've helped, but I still don't ever get $error set to anything.Here is the whole code:

// Validate form startif ($_POST) {	$username = $_POST['username']; 	$password = $_POST['password'];	$firstName = $_POST['fname'];	$lastName = $_POST['lname'];	$passwordb = $_POST['passwordb'];	$email = $_POST['email'];	$###### = $_POST['######'];	$age = $_POST['age'];	$country = $_POST['country'];	$ip = $_POST['ip'];		// validate		/* CHECK FOR FILLED FIELDS */  if ( ($username || $password || $firstName || $lastName || $passwordb || $email) == "") {		$error = '1';	}	else {		$error = '0';	}	/* CHECK FOR FILLED FIELDS */		/* EMAIL VALIDATION */  if ( filter_var($email, FILTER_VALIDATE_EMAIL)  == TRUE) {  $email = $email;  }  else {	$error = '2';  }  // Filter and Split Email    $splitemail =  explode('@', $email);   if(checkdnsrr($splitemail[1])) {	 $email = mysql_real_escape_string($email);  }  else {	$error = '2';  }   /* EMAIL VALIDATION */    if($error = '0') {	// real escape the strings:	// insert data to DB	$query = sprintf("INSERT INTO customers(username,password,firstName,lastName,######,age,email,country,ip) VALUES ('$username','$password','$firstName','$lastName','$######','$age','$email','$country','$ip');",					mysql_real_escape_string(md5($password)));					mysql_query($query);					$error = '99';  }  }?>

Link to comment
Share on other sites

Your very last IF statement is assigning 0 to $error instead of checking for it. Change it to if ($error == '0') {, note the two equal signs.I'm just making an observation here but variables should start with a letter and not with any other symbol. So you can't do $####, try to change it to a word that has meaning.

Link to comment
Share on other sites

I'm just making an observation here but variables should start with a letter and not with any other symbol. So you can't do $####, try to change it to a word that has meaning.
I'm guessing the forum automatically censored the original variable name. I think it's supposed to be that little 3-letter word for gender...:)
Link to comment
Share on other sites

I'm guessing the forum automatically censored the original variable name. I think it's supposed to be that little 3-letter word for gender...:)
Yea, it is. :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...