Jump to content

Stop and validate empty fields - php


cobusbo

Recommended Posts

Hi I'm currently having a problem with my form. Users submit an empty field into the database and the next time another user tries to enter it just says username has been taken. I need some help on how to confirm that the username and email field isnt empty when inserted and that the email address is in the correct format.

<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
 
<title>User Management System (Tom Cameron for NetTuts)</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
     $username = mysql_real_escape_string($_POST["username"]);
$email = mysql_real_escape_string($_POST["email"]);








$mxitid = mysql_real_escape_string($_SERVER["HTTP_X_MXIT_USERID_R"]);
if(!isset($mxitid))
{
	$mxitid = "DEFAULT";
}

     $checkusername = mysql_query("SELECT * FROM Users WHERE mxitid = '".$mxitid."'");
      $checkemail = mysql_query("SELECT * FROM Users WHERE email = '".$email."'");

     if(mysql_num_rows($checkusername) == 1)
     {
        echo "<h1>Error</h1>";
        echo "<p>Sorry, that username is taken. Please go <a href=\"register.php\">back</a>and try again.</p>";
     }
elseif(mysql_num_rows($checkemail) == 1)
     {
        echo "<h1>Error</h1>";
        echo "<p>Sorry, that email is taken. Please go <a href=\"register.php\">back</a>and try again.</p>";
     }

     elseif ($_POST["register"]) 
     {
$username = mysql_real_escape_string($_POST["username"]);
if(!isset($mxitid))

$mxitid = mysql_real_escape_string($_SERVER["HTTP_X_MXIT_USERID_R"]);
if(!isset($mxitid))
{
	$mxitid = "DEFAULT";
}

        $registerquery = mysql_query("INSERT INTO Users (Username,mxitid,email) VALUES('".$username."','".$mxitid."','".$email."')");
        if($registerquery)
        {
            echo "<h1>Success</h1>";
            echo "<p>Your account was successfully created. Please <a href=\"index9.php\">click here to start chatting</a>.</p>";
        }
     }

else
{
    ?>
     
   <h1>Register</h1>
     
   <p>Please enter your details below to register.</p>
<p>Keep it clean! or you might get banned!</p>

     
    <form method="post" action="register.php" name="registerform" id="registerform">
    <fieldset>
        <label for="username">Username:<br>
Using emoticons in your name won't work!</label><input type="text" name="username" id="username" /><br />
<label for="email">Email:<br>(We need your real one to be able to contact you!)<br>
 If you don't have one make use of your mxit email service.</label><input type="email" name="email" id="email" /><br />

        <input type="hidden" name="mxitid" id="mxitid" value="<? $_SERVER['HTTP_X_MXIT_USERID_R']; ?>"/><br />
        <input type="submit" name="register" id="register" value="Register" />
    </fieldset>
    </form>
     
    <?php
}
?>
 
</div>
</body>
</html>

I tried using

function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        die($problem);
    }
    return $data;
}

but it didnt seems to work

Link to comment
Share on other sites

Before anything gets added to the database, you could check if the username (or email) is empty by doing something like this:

<?php
if($username != '') {
    //continue processing form
} else {
    //let user know that $username can't be blank
}

Of course, you'll want to adapt the code so that it checks all the necessary form fields before the script saves anything to the database.

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.