Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

PHP empty(), checking for blank form fields


  • Please log in to reply
1 reply to this topic

#1 g2gsr93

g2gsr93

  • Members
  • 21 posts
  • OFFLINE
  •  
  • Local time:01:37 AM

Posted 01 July 2010 - 12:25 PM

I have a form that when submitted either creates a new user with a temp password or returns some error messages based on fields being empty or the user already existing etc. My problem is checking if the fields are blank. I'm first setting all the form fields as variables, then running an if(empty($var1)||empty($var2)){send error} check for thing. No matter what I've tried it keeps creating new users if one, some, or all fields are blank. Any ideas? Here is the part of my code that runs when the form is submitted.
[codebox]$repnamelast = $_POST['repNameLast'];
$repnamefirst = $_POST['repNameFirst'];
$repphone = $_POST['repPhone'];
$repemail = $_POST['repEmail'];
$repcheck = mysql_query("SELECT repUserName FROM salesrep")
or die(mysql_error());
while($row = mysql_fetch_array($repcheck))
{
if($row == $_POST['repUserName'])
{
$_SESSION['addrep'] = 3;
header:("Location:http:adminrepsadmin.php");
}
}
$repusername = $_POST['repUserName'];
$repconfirm = $_POST['repUserNameconfirm'];
function gen_md5_password($len)
{
return substr(md5(rand().rand()), 0, $len);
}
$temppass = gen_md5_password(5);
$insertrep = "INSERT INTO salesrep (repNameLast, repNameFirst, repPhone, repEmail, repUserName, repPassword)
VALUES ('$repnamelast', '$repnamefirst', '$repphone', '$repemail', '$repusername', '$temppass')";
if(empty($repnamefirst) || empty($repnamelast) || empty($repphone) || empty($repemail) || empty ($repusername) || empty($repconfirm))
{
$_SESSION['addrep'] = 4;
header("Location:http:adminrepsadmin.php");
}
if($repusername == $repconfirm)
{
mysql_query($insertrep) or die(mysql_error());
$_SESSION['addrep'] = 2;
header("Location:http:adminrepsadmin.php");
}
else
{
$_SESSION['addrep'] = 5;
header("Location:http:adminrepsadmin.php");
}[/codebox]

Basically I would like the if(empty....) section to change the addrep variable to = 4 (if one of the fields was empty), return you to the form page where the addrep variable of 4 will echo the proper error message. However, the empty calls are not working for me the way I thought that they would.

Edited by g2gsr93, 01 July 2010 - 12:30 PM.


BC AdBot (Login to Remove)

 


#2 g2gsr93

g2gsr93
  • Topic Starter

  • Members
  • 21 posts
  • OFFLINE
  •  
  • Local time:01:37 AM

Posted 01 July 2010 - 03:10 PM

I figured out the problem. The code that created the user was after the empty error checking, so even though it was finding that things were empty it was still creating the user. I just turned the create a user code into a function and then nested that function in a long if..elseif..else call where the createuser function is after all the error checking and only runs if all the errors return false. Phew, crazy stuff. Thanks for looking at my code if you did!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users