I am inserting values into a MySQL table using the code below, and I need to prevent users from inserting the same priority
value for each studentname
more than once. What is the best way to do this please?
我使用下面的代码将值插入MySQL表,我需要阻止用户多次为每个学生名插入相同的优先级值。请问最好的方法是什么?
This is the table:
这是表:
id studentname title academicdiscipline priority
012345678 TEST, NAME Test title 1 Civil 1
012345678 TEST, NAME Test title 2 Civil 2
012345678 TEST, NAME Test title 3 Civil 3
012345678 TEST, NAME Test title 4 Civil 4
012345678 TEST, NAME Test title 5 Civil 5
Code:
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
$ids = $_POST['id'];
$names = $_POST['studentname'];
$titles = $_POST['title'];
$disciplines = $_POST['academicdiscipline'];
$priorities = $_POST['priority'];
foreach($priorities as $key => $priority) {
if ($priority > 0) {
$query = "INSERT INTO flux_project_selection (id, studentname, title, academicdiscipline, priority)
VALUES ( " . mysql_real_escape_string($ids[$key]) . ",
'" . mysql_real_escape_string($names[$key]) . "',
'" . mysql_real_escape_string($titles[$key]) . "',
'" . mysql_real_escape_string($disciplines[$key]) . "',
" . mysql_real_escape_string($priority) . " )";
$retval = mysql_query($query) or die(mysql_error());
}
}
echo "<p> Added.</p><br />";
}
2 个解决方案
#1
2
Create index for unique record, using combination of fields
使用字段组合为唯一记录创建索引
ALTER TABLE table_name ADD UNIQUE INDEX index_name (field1, field2);
it will allow you to add first record but on duplicate entry show Duplicate entry ... for unique index. You can either use try and catch
to display error message or use on duplicate update record
if your project has this requirement.
它将允许您添加第一条记录,但在重复条目显示重复条目...为唯一索引。如果您的项目有此要求,您可以使用try和catch来显示错误消息,也可以在重复更新记录上使用。
#2
0
query for the max priority for the student then increment it with 1.
查询学生的最高优先级然后用1递增。
$queryMaxPriority = 'select max(priority) from flux_project_selection where id = STUDENT_ID;
$priority_no = $queryMaxPriority + 1;
i used this in some of my apps i developed.
我在我开发的一些应用程序中使用了这个。
Update
$priority = 'select count(priority) from flux_project_selection where id = STUDENT_ID and priority = PRIORITY';
if (count($priority>0)){
//send some error message in your page.
}
#1
2
Create index for unique record, using combination of fields
使用字段组合为唯一记录创建索引
ALTER TABLE table_name ADD UNIQUE INDEX index_name (field1, field2);
it will allow you to add first record but on duplicate entry show Duplicate entry ... for unique index. You can either use try and catch
to display error message or use on duplicate update record
if your project has this requirement.
它将允许您添加第一条记录,但在重复条目显示重复条目...为唯一索引。如果您的项目有此要求,您可以使用try和catch来显示错误消息,也可以在重复更新记录上使用。
#2
0
query for the max priority for the student then increment it with 1.
查询学生的最高优先级然后用1递增。
$queryMaxPriority = 'select max(priority) from flux_project_selection where id = STUDENT_ID;
$priority_no = $queryMaxPriority + 1;
i used this in some of my apps i developed.
我在我开发的一些应用程序中使用了这个。
Update
$priority = 'select count(priority) from flux_project_selection where id = STUDENT_ID and priority = PRIORITY';
if (count($priority>0)){
//send some error message in your page.
}