[Solved] Preventing duplicates in the database [closed]


I update your code.

<?php
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("database") or die(mysql_error());
?>
<html>
<head>
<title>Student list</title>
<link href="https://stackoverflow.com/questions/11200978/stylesheets/public.css" media="all" rel="stylesheet" type="text/css"/>
</head>
<body id="background">
<table >
  <tr>
    <td><img src=" images/Picture3.png" width="1300" height="150"/></td>
  </tr>
</table>
<table>
  <tr>
    <td id="structure">
    <? 
    // check for post data
    if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['shift']) && isset($_POST['class']) && isset($_POST['id']) )
    {
        $name=$_POST['name']; 
        $email=$_POST['email']; 
        $shift=$_POST['shift'];
        $class=$_POST['class'];
        $id=$_POST['id'];

        $username = $_POST['name']; // you must escape any input. Remember.

        $query = "SELECT * FROM `data` WHERE `name` = '".$username."'";

        $result = mysql_query($query);
        // check for duplicate
        if ( mysql_num_rows ( $result ) > 1 )
        {
            echo 'Username already exists';
        }
        else
        {
            // insert new record
            mysql_query("INSERT INTO `data`(name,email,shift,class) VALUES ('".$name."', '".$email."', '".$shift."','".$class."')");  
            print "Your information has been successfully added to the database."; 
        }
    }

    // list data
    $data = mysql_query("SELECT * FROM data")  or die(mysql_error()); 
    print "<table border cellpadding=5>"; 
    while($info = mysql_fetch_array( $data )) 
    {
        print "<tr>";
        print "<th>Id:</th><td>".$info['id']."</td>";
        print "<th>Name:</th> <td>".$info['name'] . "</td> "; 
        print "<th>Email:</th> <td>".$info['email'] . " </td>";
        print "<th>shift:</th> <td>".$info['shift'] . " </td>";
        print "<th>class:</th> <td>".$info['class'] . " </td>";
        print "<tr><td><a href="update.php?id={$info["id']}'>EDIT</a></td></tr>";
        print "<tr><td><a href="delete.php?id={$info["id']}'>DELETE</a></td></tr>";
    } 
    print "</table>"; 

  ?>
  </td>
  </tr>
</table>
</body>
</html>

3

solved Preventing duplicates in the database [closed]