Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What\'s wrong with the code? The code can be seen below the submit button on the

ID: 3879210 • Letter: W

Question

What's wrong with the code? The code can be seen below the submit button on the page


   

     

Registration Form


     
     


     
     
     


     
     


     


     

Rasmussen Registration Form


     

Fill in your name and telephone, then click Submit to register.


     


      FIRST_NAME

      LAST_NAME

      TELEPHONE

     
     

            // DB connection info
      // Within the PHP tags, add PHP code for connecting to the database.
      $host = " us-cdbr-azure-central-a.cloudapp.net ";
      $user = "b207b67c4c62ac";
      $pwd = "b8db6089";
      $db = "cda";

      // Connect to Database.
      try {
        $conn = new PDO( "mysql:host=$host;dbname=$db", $user, $pwd);
        $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
      }
      catch(Exception $e){
        die(var_dump($e));
      }
      //Following the database connection code, add code for inserting registration information into the database.
      if(!empty($_POST)) {
        try {
          $first_name = $_POST['first_name'];
          $last_name = $_POST['last_name'];
          $telephone = $_POST['telephone'];
          // Insert data
          $sql_insert = "INSERT INTO test (FIRST_NAME, LAST_NAME, TELEPHONE)
          VALUES (?,?,?)";
          $stmt = $conn->prepare($sql_insert);
          $stmt->bindValue(1, $first_name);
          $stmt->bindValue(2, $last_name);
          $stmt->bindValue(3, $telephone);
          $stmt->execute();
        }
        catch(Exception $e) {
          die(var_dump($e));
        }
        echo "

You're Registered!

";
      }
      //Finally, following the code above, add code for retrieving data from the database.
      $sql_select = "SELECT * FROM test";
      $stmt = $conn->query($sql_select);
      $registrants = $stmt->fetchAll();
      if(count($registrants) > 0) {
        echo "

People Who Are Registered:

";
        echo "";
        echo "";
        echo "";
        echo "";
        foreach($registrants as $registrant) {
          echo "";
          echo "";
          echo "";
        }
        echo "

";
      } else {
        echo "

No one is currently registered.

";
      }

      ?>

     

    


     


     

This is a block paragraph #2


     

     


     

This is a block paragraph #3


     
    
     
     
     

FIRST_NAME LAST_NAME TELEPHONE ".$registrant['FIRST_NAME']." ".$registrant['LAST_NAME']." ".$registrant['TELEPHONE']."

Explanation / Answer

HTML code

<form name="frmRegistration" method="post" action="">

<table border="0" width="500" align="center" class="demo-table">

<?php if(!empty($success_message)) { ?>

<div class="success-message"><?php if(isset($success_message)) echo $success_message; ?></div>

<?php } ?>

<?php if(!empty($error_message)) { ?>

<div class="error-message"><?php if(isset($error_message)) echo $error_message; ?></div>

<?php } ?>

<tr>

<td>User Name</td>

<td><input type="text" class="demoInputBox" name="userName" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>"></td>

</tr>

<tr>

<td>First Name</td>

<td><input type="text" class="demoInputBox" name="firstName" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>"></td>

</tr>

<tr>

<td>Last Name</td>

<td><input type="text" class="demoInputBox" name="lastName" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName']; ?>"></td>

</tr>

<tr>

<td>Password</td>

<td><input type="password" class="demoInputBox" name="password" value=""></td>

</tr>

<tr>

<td>Confirm Password</td>

<td><input type="password" class="demoInputBox" name="confirm_password" value=""></td>

</tr>

<tr>

<td>Email</td>

<td><input type="text" class="demoInputBox" name="userEmail" value="<?php if(isset($_POST['userEmail'])) echo $_POST['userEmail']; ?>"></td>

</tr>

<tr>

<td>Gender</td>

<td><input type="radio" name="gender" value="Male" <?php if(isset($_POST['gender']) && $_POST['gender']=="Male") { ?>checked<?php } ?>> Male

<input type="radio" name="gender" value="Female" <?php if(isset($_POST['gender']) && $_POST['gender']=="Female") { ?>checked<?php } ?>> Female

</td>

</tr>

<tr>

<td colspan=2>

<input type="checkbox" name="terms"> I accept Terms and Conditions <input type="submit" name="register-user" value="Register" class="btnRegister"></td>

</tr>

</table>

</form>

And the styles are

.error-message {

padding: 7px 10px;

background: #fff1f2;

border: #ffd5da 1px solid;

color: #d6001c;

border-radius: 4px;

}

.success-message {

padding: 7px 10px;

background: #cae0c4;

border: #c3d0b5 1px solid;

color: #027506;

border-radius: 4px;

}

.demo-table {

background: #d9eeff;

width: 100%;

border-spacing: initial;

margin: 2px 0px;

word-break: break-word;

table-layout: auto;

line-height: 1.8em;

color: #333;

border-radius: 4px;

padding: 20px 40px;

}

.demo-table td {

padding: 15px 0px;

}

.demoInputBox {

padding: 10px 30px;

border: #a9a9a9 1px solid;

border-radius: 4px;

}

.btnRegister {

padding: 10px 30px;

background-color: #3367b2;

border: 0;

color: #FFF;

cursor: pointer;

border-radius: 4px;

margin-left: 10px;

}

PHP Registration Form Input Validation

/* Form Required Field Validation */

foreach($_POST as $key=>$value) {

if(empty($_POST[$key])) {

$error_message = "All Fields are required";

break;

}

}

/* Password Matching Validation */

if($_POST['password'] != $_POST['confirm_password']){

$error_message = 'Passwords should be same<br>';

}

/* Email Validation */

if(!isset($error_message)) {

if (!filter_var($_POST["userEmail"], FILTER_VALIDATE_EMAIL)) {

$error_message = "Invalid Email Address";

}

}

/* Validation to check if gender is selected */

if(!isset($error_message)) {

if(!isset($_POST["gender"])) {

$error_message = " All Fields are required";

}

}

/* Validation to check if Terms and Conditions are accepted */

if(!isset($error_message)) {

if(!isset($_POST["terms"])) {

$error_message = "Accept Terms and Conditions to Register";

}

}

Add Registered User Data to Database
After successful PHP validation, an insert query is created by using the registration data to store it into the database table. The password is encrypted using md5 function for the security.

if(!isset($error_message)) {

require_once("dbcontroller.php");

$db_handle = new DBController();

$query = "INSERT INTO registered_users (user_name, first_name, last_name, password, email, gender) VALUES

('" . $_POST["userName"] . "', '" . $_POST["firstName"] . "', '" . $_POST["lastName"] . "', '" . md5($_POST["password"]) . "', '" . $_POST["userEmail"] . "', '" . $_POST["gender"] . "')";

$result = $db_handle->insertQuery($query);

if(!empty($result)) {

$error_message = "";

$success_message = "You have registered successfully!";

unset($_POST);

} else {

$error_message = "Problem in registration. Try Again!";

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote