我正在製作一個簡單的學生管理系統,它有四種形式。我怎樣才能防止數據庫中的重複條目?我想防止重複,我應該在代碼中添加什麼?防止數據庫中的重複
<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>
爲什麼我們在等待 – McGarnagle
代碼轉儲不是一個非常有用的問題。我建議首先使用谷歌搜索你的問題 - [第一個結果]之一(http://pb.zambale.com/2012/01/25/mysql-prevent-duplicates-using-the-unique-constraint/)非常適用。 – jmdeldin
@dbaseman:因爲它是緊急的!! 1 – jmdeldin