How to allow the visitor to a page if they have entered only full registration number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bala Kumaran
    New Member
    • Jan 2013
    • 30

    #1

    How to allow the visitor to a page if they have entered only full registration number

    Hi Experts,

    I've created a resulting script like this

    search.php
    Code:
    <?php
    
    // Make a MySQL Connection
    mysql_connect("localhost", "db_user_name", "db_user_password) or die(mysql_error());
    mysql_select_db("db_name") or die(mysql_error());
    
        $query = $_GET['query']; 
        
         
        $min_length = 3;
        
         
        if(strlen($query) >= $min_length){ 
             
            $query = htmlspecialchars($query); 
            
             $query = mysql_real_escape_string($query);
    
             $raw_results = mysql_query("SELECT * FROM students_mark_table
                WHERE (`reg_number` LIKE '%".$query."%')" or die(mysql_error());
             
            if(mysql_num_rows($raw_results) > 0){
                 
                while($results = mysql_fetch_array($raw_results)){
                 
                 echo "<p><h3>Registration&nbsp;Number:&nbsp;".$results['reg_number']."</h3><br></br><h3>Name:&nbsp;".$results['name']."</h3><br></br><h3>Subject:&nbsp;".$results['subject']."</h3><br></br><h3>Marks:&nbsp".$results['marks']."</h3></p>";
                }
                 
            }
            else{ 
                echo "You've Entered incorrect registration number or The number may not existed"
            }
             
        }
        else{
            echo "Minimum length is ".$min_length;
        }
    ?>

    The Sql table Data's like this....

    Table Name is /--->students_mark_ table<----\

    It is having, reg_number, name, subject, marks.



    The input section like this
    index.php

    Code:
    <h1>Enter Your REGISTRATION NUMBER in the box</h1><form name="myForm" action="search.php" onsubmit="return validateForm()" method="GET"><center> <input name='query' onblur='if (this.value == &quot;&quot;) {this.value = &quot;Enter Your REGISTRATION NUMBER&quot;;}' onfocus='if (this.value == &quot;Enter Your REGISTRATION NUMBER&quot;) {this.value = &quot;&quot;}' size='30' type='text' value=''/>  
      <input class='submit' type='submit' value='Submit'/></center></form>
    In the My Sql table all registration numbers of the student are 6 digit numbers.

    In the sql table there is a register number existed like this "405060". When I Enter the registration number(405060) in the form in index.php page, The output of the page is

    Registration Number: 405060
    Name: BALA
    Subject: ENGLISH
    Marks: 98

    But, IF I've entered few charactors of the registration Number "4050" or "40506" It also shows the result of the Registration Number "405060".

    How to I block the output from entering few charactors. Only allow if the full charactors entered.

    There is more than registration number existed in the sql same series of the registration number like this "405067", "405062". If the visitor entered few charactors like this "40506" It shows the output of all registration number of the same series.

    How to I solve this.. please help me..
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2433

    #2
    Edit the following line:

    Code:
     
    $raw_results = mysql_query("SELECT * FROM students_mark_table
                WHERE (`reg_number` LIKE '%".$query."%')" or die(mysql_error());
    to:

    Code:
     $raw_results = mysql_query("SELECT * FROM students_mark_table
                WHERE (`reg_number` LIKE '".$query."')" or die(mysql_error());
    The removal of "%" before and after says you want an exact match returned.
    niheel @ bytes

    Comment

    • Bala Kumaran
      New Member
      • Jan 2013
      • 30

      #3
      Thank YOU............ . :)

      Comment

      Working...