-3

I am trying to register a new user by posting their form data to the database via a php scriptregister.php but I am getting an array of errors when I hit register, the data is supposed to be validated by a second script called validate.php. My register.php is shown below. Same errors exist when the form is empty and when is filled.

<?php require('scripts/validate.php');?>
<!DOCTYPE html>
<html>
<head>

  <meta charset="UTF-8" />
  <title>Register</title>

<body>
 <div id="mainWrapper">
    <div id="register">
        <?php if(isset($error)){echo "<div id='error'>".$error."</div>";}?>
        <?php if(isset($success)){echo "<div id='success'>".$success."</div>";}?>
      <form method="post" action="" >
        <fieldset>
            <legend>Register Here</legend>
            <p>
            <label for="Username">Username</label>
            <input type="text" id="username"/>
            </p>
            <p>
            <label for="Email">Email</label>
            <input type="text" id="email"/>
            <p>
            <label for="Firstname">Firstname</label>
            <input type="text" id="firstname"/>
            </p>
            <p>
            <label for="Lastname">Lastname</label>
            <input type="text" id="lastname"/>
            </p>
            <p>
            <label for="Password">Password</label>
            <input type="password" id="password"/>
            </p>
            <p>
            <label for="Re-Type Password">Re-Type Password</label>
            <input type="password" id="password2"/>
            </p>
            <p>
            <label for="DOB">DOB</label>
            <input type="text" id="dob">
            </p>
            <p>
            <label for="Adress">Adress</label>
            <input type="text" id="address"/>
            </p>
            <p>
            <label for="Adress 2">Adress 2</label>
            <input type="text" id="address2"/>
            </p>
            <p>
            <label for="town">Town</label>
            <input type="text" id="town"/>
            </p>
            <p>
            <label for="county">County</label>
            <input type="text" id="county"/>
            </p>
            <p>
            <label for="Postalcode">PostalCode</label>
            <input type="text" id="postcode"/>
            </p>
            <p>
            <label for="contactno">Contact No.</label>
            <input type="text" id="contact"/>
            </p>
          <input type="submit" name="submit" value="Register"/>
            </fieldset>
            </form>
            </div>
        </div>
</body>
</html>

And the validate.php is here

   <?php include('connection.php');?>
    <?php
    if(isset($_POST['submit']))
    {

    $username=$_POST['username'];
    $email=$_POST['email'];
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $password=$_POST['password'];
    $password2=$_POST['password2'];
    $dob=$_POST['dob'];
    $address=$_POST['address'];
    $address2=$_POST['address2'];
    $town=$_POST['town'];
    $county=$_POST['county'];
    $postcode=$_POST['postcode'];
    $contact=$_POST['contact'];

    $fetch=mysql_query("SELECT id FROM users WHERE email='$email'")or die(mysql_error());
    $num_rows=mysql_num_rows($fetch);

    if(empty($username)||empty($email) || empty($firstname) || empty($lastname) || empty($password) || empty($password2)  || empty($dob) || empty($address) || empty($town)|| empty($postcode) || empty($contact))
    {
        $error= 'ALl * fields are required';
    }
    elseif (!filter_var($email,FILTER_VALIDATE_EMAIL)) 
    {
    $error= 'A valid email is required';
    }
    elseif (!empty($contact)) 
    {
       if (!is_numeric($contact)) 
         {
           $error= 'Enter a valid contact No.';
         }  
    }
    elseif ($password !=$password2) 
    {
    $error= "Passwords don't match";
    }
    elseif ($num_rows >=1)
     {
        $error='We already have this email registered,try a new one!';
     }
    else
        {
            $password=md5($password);
            $sql=mysql_query("INSERT INTO user(username,email,firstname,lastname,password,dob,address,address2,town,county,postcode,contact)VALUES('$username','$email','$firstname','$lastname','$password','$dob','$address','$address2','$town','$county','$postcode','$contact')");
        if($sql)
        {
    header("location:login.php");   
        }
       }
}
?>

I'll greatly appreciate guys.

AkshayP
  • 2,141
  • 2
  • 18
  • 27
user3725685
  • 1
  • 1
  • 1
  • 1
  • No `name`, no values... – War10ck Jun 10 '14 at 20:58
  • Thanks guys, I eliminated those errors. But still i have a problem, the form doesn't add to the database and am getting this error form the connection script(connection.php) Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\Registration\scripts\connection.php on line 7.here i s the connection script – user3725685 Jun 11 '14 at 03:29

6 Answers6

3

Give your inputs a name property.

E.g:

<input type="text" id="username" name="username" />

The issue is that it's looking for name, but it doesn't exist.

This goes for all of your input fields, not just that specific one and not just for type="text".

Albzi
  • 15,431
  • 6
  • 46
  • 63
0

When you POST data using a form, you need to specify the name of the data using the name attribute. Replace all of the id attributes with name and your form will work. For example:

<input type="text" id="username"/>

should become:

<input type="text" name="username"/>

edcs
  • 3,847
  • 2
  • 33
  • 56
0

Replace all elements' id with name. When form is submitted the element's information is submitted with associated name and not id. e.g. <input type="text" id="username" name="username"/>

v2solutions.com
  • 1,439
  • 9
  • 8
0

This isn't a PHP error, it's a HTML one.

POSTS variables are stated using the name tag in HTML, not the id tag.

Your inputs should look like this:

<input type="text" name="username"/>

Just change the id tags to name tags, and it should work.

Scarraban
  • 106
  • 5
0

Change your HTML Form

 <?php require('scripts/validate.php');?>
    <!DOCTYPE html>
    <html>
    <head>

      <meta charset="UTF-8" />
      <title>Register</title>

    <body>
     <div id="mainWrapper">
        <div id="register">
            <?php if(isset($error)){echo "<div id='error'>".$error."</div>";}?>
            <?php if(isset($success)){echo "<div id='success'>".$success."</div>";}?>
          <form method="post" action="scripts/validate.php" >
            <fieldset>
                <legend>Register Here</legend>
                <p>
                <label for="Username">Username</label>
                <input type="text" id="username" name="username"/>
                </p>
                <p>
                <label for="Email">Email</label>
                <input type="text" id="email" name="email"/>
                <p>
                <label for="Firstname">Firstname</label>
                <input type="text" id="firstname" name="firstname"/>
                </p>
                <p>
                <label for="Lastname">Lastname</label>
                <input type="text" id="lastname" name="lastname"/>
                </p>
                <p>
                <label for="Password">Password</label>
                <input type="password" id="password" name="password"/>
                </p>
                <p>
                <label for="Re-Type Password">Re-Type Password</label>
                <input type="password" id="password2" name="password2"/>
                </p>
                <p>
                <label for="DOB">DOB</label>
                <input type="text" id="dob" name="dob">
                </p>
                <p>
                <label for="Adress">Adress</label>
                <input type="text" id="address" name="address"/>
                </p>
                <p>
                <label for="Adress 2">Adress 2</label>
                <input type="text" id="address2" name="address2"/>
                </p>
                <p>
                <label for="town">Town</label>
                <input type="text" id="town" name="town"/>
                </p>
                <p>
                <label for="county">County</label>
                <input type="text" id="county" name="county"/>
                </p>
                <p>
                <label for="Postalcode">PostalCode</label>
                <input type="text" id="postcode" name="postcode"/>
                </p>
                <p>
                <label for="contactno">Contact No.</label>
                <input type="text" id="contact" name="contact"/>
                </p>
              <input type="submit" name="submit" value="Register"/>
                </fieldset>
                </form>
                </div>
            </div>
    </body>
    </html>
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
0

as others have stated here, your form elements need a name attribute. the id attribute is great for referencing the form elements in JavaScript or CSS. but with $_POST variables you need to specify the name. Hope this gives an understanding to why you need the name attribute instead of the id attribute.

jfh6
  • 23
  • 6