• Finding Duplicate Records in MySQL using PHP

    Below is code to test if the record is already exist in the MySQL database.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
     
    <!--?php 
    $id = $_REQUEST['id'];
    $first_name = strtoupper($_REQUEST['first_name']);
    $last_name = strtoupper($_REQUEST['last_name']);
     
    $connet=mysql_connect("localhost","root","") or die(mysql_error());
    $db=mysql_select_db("student") or die(mysql_error());
     
    // Code for checking if the text field is empty using
    // different conditions
     
    $id_no = (empty($id));
    $first_name2 = (empty($first_name)) ;
    $last_name2 = (empty($last_name));
     
    if ($id_no == true &#038;&#038; $first_name2 == true
    &#038;&#038; $last_name2 == true)
    {
    echo "<h1--> Please filled out the form correctly!";
    }
    else if ($first_name2 == true &amp;&amp; $last_name2 == true)
    {
    echo "

    Both first name and last name is not filled out correctly!

    1
    2
    3
    4
    5
    
    ";
    }
    else if ($id_no == true &amp;&amp; $first_name2 == true)
    {
    echo "

    Both ID No. and First Name is not filled out correctly!

    1
    2
    3
    4
    5
    
    ";
    }
    else if ($id_no == true &amp;&amp; $last_name2 == true)
    {
    echo "

    Both ID No. and Last Name is not filled out correctly!

    1
    2
    3
    4
    5
    6
    
    ";
    }
     
    else if ($id_no == true)
    {
    echo "

    The student ID No. is not filled out correctly!

    1
    2
    3
    4
    5
    
    ";
    }
    else if ($first_name2 == true)
    {
    echo "

    The student first name is not filled out correctly!

    1
    2
    3
    4
    5
    
    ";
    }
    else if ($last_name2 == true)
    {
    echo "

    The student last name is not filled out correctly!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    ";
    }
     
    // Code for checking for duplicate record in the database
    // for the first name and the last name of the student.
     
    else if (mysql_num_rows(mysql_query("SELECT first_name,last_name
    FROM info WHERE first_name = '$first_name' AND
    last_name = '$last_name'"))) {
    echo "

     

    Sorry Duplicate Record for ”
    .$first_name. ” ” .$last_name. “. “;
    echo “

     

    Please Try Again. “;

    }
    else {
    $sql = “INSERT INTO info(id,first_name,last_name)
    VALUES (‘$id’,'$first_name’,'$last_name’)”;
    $query = mysql_query($sql) or die(mysql_error());
    echo ”

     

    The record of ”
    .$first_name. ” ” .$last_name.
    ” has been saved in the database.”;
    }
    ?>

     

     

Comments on this post

Leave a Reply

  • Security Code :


    − one = 8