Jump to content

if($ == ""){} help...


Vivid Lust

Recommended Posts

Hi, here's the following code i'm using at the moment:

 

<?php
//require files
require("includes/db.php");

// get form data
$name   = trim(strip_tags($_POST['name']));
$email  = $_POST['email'];
$pass   = md5($_POST['pass']);
$pass2   = md5($_POST['pass2']);

//validate blank fields
if($name == "" || $email == "" || $pass == "" || $pass2 == ""){
header('location:register.php?msg=1'); 
exit; 
}
else{
if ($pass == $pass2) {
    $user = mysql_query("SELECT * FROM users WHERE (email='$email')");
    if (mysql_num_rows($user) > 0) {
        header("location:register.php?msg=3");
        
    }
    else {
        $user = mysql_query("
        INSERT INTO `users` (
`email` ,
`pass` ,
`name`
)
VALUES (
\"$email\", \"$pass\", \"$name\"
)

        ");

          header("location:success.php"); 
    }
}
else {
    header("location:register.php?msg=2");
} 
}
?>

 

The problem is, is that if the name or email field is blank, then the page is redirected however if a pass or pass2 is blank then it doesnt redirect and either says the passwords dont match (if only pass or pass2 is filled in); or if they are both left blank then it doesnt recognise that there is no data in the field.

 

Please help!

 

Thanks if you can in advanced.

Link to comment
Share on other sites

There is no misspell...

 

Here's the code for the registration page if you need it:

 

<?php
// set variables
$get = $_GET['msg']; 
// display relevant message based on number
if($get == "1"){
    $msg = "Please fill in the form correctly"; 
}
if($get == "2"){
    $msg = "Passwords didn't match"; 
}
if($get == "3"){
    $msg = "This email is already in use, please use another"; 
}
?>
<?php echo $msg; ?> 
<form method="post" action="doregister.php">
Name<br />
<input type="text" name="name" /><br />
Email<br />
<input type="text" name="email" /><br />
Password<br />
<input type="password" name="pass" /><br />
Password (again)<br />
<input type="password" name="pass2" /><br />
<input type="submit" value="Register" />
</form>
All fields are required. 

Link to comment
Share on other sites

I would use && instead of ||, and I would make the string for your query a variable, plus I would take a look at some of your syntax (most particularly your spacing) other then that I don't exactly see why its not working, although that could very well be, because of the fact that I'm very tired

Link to comment
Share on other sites

basically what you are saying is that

 

if

    username

    or

    email

    or

    pass

    or

  pass2

 

are empty then redirect them.

 

so in ur case if any one of the fields is empty then u will always be redirected however

 

u have said $pass= md5($_post['pass']);

 

althought the entry is blank ur encoding this which means $pass is not going to be empty may have a longer number and also do put queries on variables to save the hassle lol like burn1337 sais

Link to comment
Share on other sites

try this

 

 


<?php
//require files
require("includes/db.php");

// get form data
$name   = trim(strip_tags($_POST['name']));
$email  = $_POST['email'];
$pass   = md5($_POST['pass']);
$pass2   = md5($_POST['pass2']);

//validate blank fields
if($name == "" || $email == "" ){
header('location:register.php?msg=1'); 
exit; 
}
elseif($pass == "" || $pass2 == ""){
            header('location:register.php?msg=1'); 
exit; 
}
else{

cuntinue the same code u had.....




 

 

 

if it doesnt work the try if its empty function

 

 

Link to comment
Share on other sites

If you run an md5 on a blank variable the result will be this:

 

d41d8cd98f00b204e9800998ecf8427e

 

So, don't run the md5() on the passwords until after you have checked if they are empty. Or instead of checking they are equal to nothing, check if they are equal to "d41d8cd98f00b204e9800998ecf8427e". If they are, they are empty.

Link to comment
Share on other sites

Thanks to valtido, i fixed it ...

 

<?php
// get form data
$name   = trim(strip_tags($_POST['name']));
$email  = $_POST['email'];
$pass   = md5($_POST['pass']);
$pass2   = md5($_POST['pass2']);
$passa   = $_POST['pass'];
$pass2a   = $_POST['pass2'];

//validate blank fields
if($name == "" ||  $email == "" ||  $passa == "" ||  $pass2a == ""){
header('location:register.php?msg=1'); 
exit;
?> 

 

I didnt see your previous post as i was writing my other one... and I missed it they have 5 seconds in between

 

thanks again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.