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