【问题标题】:Preventing duplicates in the database [closed]防止数据库中的重复[关闭]
【发布时间】:2012-06-26 04:58:35
【问题描述】:

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

<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>

【问题讨论】:

  • 代码转储不是一个非常有用的问题。我建议先用谷歌搜索你的问题——first results 之一非常适用。
  • @dbaseman:因为它很紧急!!1
  • 自 PHP 5.3 起,mysql_* 函数已被弃用,强烈建议不要依赖它们。考虑使用 PDO 或 MySQLi。

标签: php database


【解决方案1】:

我更新你的代码。

<?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>

【讨论】:

  • 看来您只是重新格式化了代码。你的贡献是什么?
  • 您纠正的具体错误是什么?枚举它们将有助于@FAWAZ 和未来的访问者。
  • 感谢大家的帮助
【解决方案2】:

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

【讨论】:

  • 我知道 UNQUE CONTRAINT 但我想在 SQL 的帮助下做到这一点
  • @FAWAZ:你为什么不想使用唯一约束?是因为插入重复记录会返回错误吗?您可以只使用选择查询来检查记录是否已存在。
  • @FAWAZ,你不能全部输入大写吗?看起来你在对人大喊大叫。
【解决方案3】:

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

当您在数据库中创建表时决定这一点后,您必须申请 unique constraint

【讨论】:

  • 我不希望该电子邮件被复制
  • 所以假设表名是用户并且它有列 id,firstname,lastname,email 它会在 mysql CREATE TABLE USER (Id int NOT NULL, LastName varchar(255) NOT NULL、FirstName varchar(255)、Email varchar(100)、UNIQUE (Email))
【解决方案4】:

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

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

如果您正在考虑以添加/插入/仅使用唯一记录的方式修改表,那么 unique constraint 很好。

问候。

---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'";

【讨论】:

  • 我已将名称存储在 $name 而不是 $username
  • 那么 $username 代表什么?还有为什么你在 Query 中使用 { } 大括号?
猜你喜欢
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多