我正在制作一个简单的学生管理系统,它有四种形式。如何防止数据库中的重复条目?我想防止重复,我应该在代码中添加什么?
<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>
答案 0 :(得分: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>
答案 1 :(得分:1)
创建数据库时,可以为任何列指定“唯一”约束。有关示例,请参阅http://www.w3schools.com/sql/sql_unique.asp
答案 2 :(得分:0)
首先,您必须确定不应复制记录的哪一列。像firstName可以有重复,所以可以是出生日期。但SSN不能重复。或者可能基于您的业务逻辑集列不能重复,如firstname,lastname,dob和fathersName的组合不能重复。
在数据库中创建表格后决定执行此操作后,您必须应用unique constraint
答案 3 :(得分:0)
如果您打算只显示唯一记录,那么:
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'";