-6

我正在制作一个简单的学生管理系统,它有四种形式。如何防止数据库中的重复条目?我想防止重复,我应该在代码中添加什么?

<html>
 <head>
      <title>Student list</title>
          <link href="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">
 <? 
  $name=$_POST['name']; 
  $email=$_POST['email']; 
  $shift=$_POST['shift'];
  $class=$_POST['class'];
  $id=$_POST['id'];
  ?><?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 ) > 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."; 
  ?>
  <?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 "<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>
4

4 回答 4

4

我更新你的代码。

<?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="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>
于 2012-06-26T07:25:42.843 回答
1

创建数据库时,可以为任何列指定“唯一”约束。例如,请参阅http://www.w3schools.com/sql/sql_unique.asp

于 2012-06-26T05:01:29.743 回答
0

首先,您必须确定不应重复记录的哪一列。就像名字可以有重复一样,出生日期也可以。但 SSN 不能重复。或者可能基于您的业务逻辑,列集不能重复,例如 firstname、lastname、dob 和 FathersName 的组合不能重复。

在您在数据库中创建表时决定这一点后,您必须应用唯一约束

于 2012-06-26T05:18:36.540 回答
0

如果您想只显示唯一记录,那么:

SELECT DISTINCT (Column1,Column2, ...) FROM Data

如果您正在考虑以添加/插入/仅使用唯一记录的方式修改表,那么 唯一约束就可以了。

问候。

---X----
在您的代码中:

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

$query = "SELECT * FROM `data` WHERE `$name` = '{name}'";

您已将名称存储在 $username 并使用 $name
它应该:

$query = "SELECT * FROM `data` WHERE `name` = '$username'";
于 2012-06-26T05:24:55.610 回答