title: Preventing duplicates in the database
body: <p>I am making a simple student management system which has four forms. How can I prevent duplicate entries in the database? I want to prevent duplicates, what I should add in ths code?</p>
  
  <pre><code>&lt;html&gt;
   &lt;head&gt;
        &lt;title&gt;Student list&lt;/title&gt;
            &lt;link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css"/&gt;
   &lt;/head&gt;
  &lt;body id="background"&gt;
    &lt;table &gt;
       &lt;tr&gt;
       &lt;td&gt;
       &lt;img src=" images/Picture3.png" width="1300" height="150"/&gt;
   &lt;/td&gt;
       &lt;/tr&gt;
    &lt;/table&gt;
               &lt;table&gt;
               &lt;tr&gt;
  
   &lt;td id="structure"&gt;
   &lt;? 
    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $shift=$_POST['shift'];
    $class=$_POST['class'];
    $id=$_POST['id'];
    ?&gt;&lt;?php
  
  
  
   $var= mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("database") or die(mysql_error());
  
  
  
  $username = $_POST['name']; // you must escape any input. Remember.
  
  $query = "SELECT * FROM `data` WHERE `$name` = '{name}'";
  
  $result = mysql_query($query);
  
  if ( mysql_num_rows ( $result ) &gt; 1 )
  {
      /* Username already exists */
      echo 'Username already exists';
  }
  else
  {
  
  
  
    mysql_query("INSERT INTO `data`(name,email,shift,class) VALUES ('$name', '$email', '$shift','$class')"); }
  
  
  
  
  
  
  
  
  
  
  
  
    Print "Your information has been successfully added to the database."; 
    ?&gt;
    &lt;?php 
    // Connects to your Database 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("database") or die(mysql_error()); 
    $data = mysql_query("SELECT * FROM data") 
    or die(mysql_error()); 
    Print "&lt;table border cellpadding=5&gt;"; 
    while($info = mysql_fetch_array( $data )) 
    {
  
    Print "&lt;tr&gt;";
    Print "&lt;th&gt;Id:&lt;/th&gt;&lt;td&gt;".$info['id']."&lt;/td&gt;";
    Print "&lt;th&gt;Name:&lt;/th&gt; &lt;td&gt;".$info['name'] . "&lt;/td&gt; "; 
    Print "&lt;th&gt;Email:&lt;/th&gt; &lt;td&gt;".$info['email'] . " &lt;/td&gt;";
    Print "&lt;th&gt;shift:&lt;/th&gt; &lt;td&gt;".$info['shift'] . " &lt;/td&gt;";
    Print "&lt;th&gt;class:&lt;/th&gt; &lt;td&gt;".$info['class'] . " &lt;/td&gt;";
    Print "&lt;tr&gt;&lt;td&gt;&lt;a href='update.php?id={$info['id']}'&gt;EDIT&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;";
    PRINT "&lt;tr&gt;&lt;td&gt;&lt;a href='delete.php?id={$info['id']}'&gt;DELETE&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;";
  
    } 
    Print "&lt;/table&gt;"; 
    ?&gt;
    &lt;/td&gt;&lt;/tr&gt;
    &lt;/table&gt;
   &lt;/body&gt;&lt;/html&gt;
  </code></pre>