PDA

View Full Version : Set empty variable to NULL


CVO
01-31-2008, 01:55 PM
I have a flash registration form and the email field is optional. Now when the user hits submit their username, password and email are added to a database. Only problem is that if the user doesn't put in their email address then it is being recorded to the database as an empty variable rather than NULL.

I've tried to have PHP check if it's an empty variable and I put the following in the script but it doesn't work:

$length=strlen($email);
if ($length=0){
$email = NULL;
}
Also tried
if ($email = ""){
$email = NULL;
}

Any idea what I'm doing wrong or is there another way?

Thanks :)

CyanBlue
01-31-2008, 01:58 PM
You probably need '==' instead of '='... ;)

CVO
01-31-2008, 02:17 PM
Thanks CyanBlue (again!) but it is still coming up empty. Here is the full script with the "=="
<?
//this pulls the variables from the flash movie when the user
//hits submit.
$user = $_POST ['user'];
$pass = $_POST ['pass'];
$email = $_POST ['email'];

//change empty email field to NULL value
$length=strlen($email);
if ($length=0){
$email == NULL;
}

//connect to database
mysql_pconnect("","") or die ("didn't connect to mysql");
mysql_select_db("") or die ("no database");

//make query
$query = "SELECT * FROM users WHERE user = '$user'";
$result = mysql_query( $query ) or die ("didn't query");

//see if user already exists in the database
$num = mysql_num_rows( $result );
if ($num == 1){
print "status=That username already exists. Please choose another...";
} else {
$query = "SELECT * FROM users WHERE email IS NOT NULL and email = '$email'";
$result = mysql_query( $query ) or die ("didn't query");
//see if email already exists in the database
$num = mysql_num_rows( $result );
if ($num == 1){
print "status=There is an account registered under that email address. To retrieve your log in details click on the BACK button...";
} else {
print "status=Registering Details...";
mysql_query ("INSERT INTO users (user, pass, email) VALUES ('$user', '$pass', '$email')");
print "status=Details successfully registered...&checklog=3";
}

}
?>

I've been trying to figure this out all last night with no success :(

CyanBlue
01-31-2008, 02:38 PM
Um... Change these...
//change empty email field to NULL value
$length=strlen($email);
if ($length=0){
$email == NULL;
}
to these and see what happens...
//change empty email field to NULL value
if (!isset($email))
{
$email = NULL;
}
Oh, when I said '==', I meant the '==' inside the if statement... ;)

CVO
01-31-2008, 04:39 PM
Tried that with the equals and the !isset but I'm still not getting NULL. Here is an image of what I get in my MySQL admin after I register a user who hasn't put in their email address: http://www.thetreasurehuntproject.com/null.jpg (the first record with the NULL email I put in myself in the admin screen and not through php).
I want users to be able to reg without their email but if 1 user has no email and another tries to register without an email then the query counts "" and "" as matching email addresses.

If you go to here (http://www.thetreasurehuntproject.com/real) and try to register as a new user without putting in an email then you will see the message on the bottom say email already exists.

As you can tell I'm no coder:o