Monday, April 11, 2016

Building a social network: checkup(P7).

Introduction:

 To go with signup.php I will continue introducing you how to write code for checkup.php file which  is used to look up the username in the database and returns a string indicating whether it has been already been taken. 

checkup.php

<?php 
  require_once 'functions.php';

  if (isset($_POST['user']))
  {
    $user   = sanitizeString($_POST['user']);
    $result = queryMysql("SELECT * FROM members WHERE user='$user'");

    if ($result->num_rows)
      echo  "<span class='taken'>&nbsp;&#x2718; " .
            "This username is taken</span>";
    else
      echo "<span class='available'>&nbsp;&#x2714; " .
           "This username is available</span>";
  }
?>

  As we can see, it relies on the functions sanitizeString and queryMysql. Therefore, the program first includes the file functions.php. 
 Then, if the $_POST variable user has a value, the function looks it up in the database and, depending on whether it exists as a username, outputs either “This username is taken” or “This username is available.” Just checking the function mysql_num_rows against the result is sufficient for this, as it will return  0  for not found, or 1 if it is found.   
 The HTML entities &#x2718; and &#x2714; are also used to preface the string with either a cross or a checkmark. Below is an example new user creates an account which is already existed in the database.

Fig. 1. Duplicated account error.

  Conclusion:

  In this section I helped you to write code for checkup.php file combined with signup.php file to do checking and creating a new user account for the site. I hope that you can find useful information in this section.
   

No comments:

Post a Comment