首先,我使用 MYSQL
,因为我大学的服务器尚未针对“mySQLI”更新其服务器
。
上面是表格,test将参加所有事件,但我不想让test再次参加事件。
if(isset($_REQUEST['register'])!=''){
$user = $name;
$eventTitle = $_REQUEST['select'];
$result = mysql_query("SELECT Event FROM attend where Event != '$eventTitle'"); // run the query and assign the result to $result
while($table = mysql_fetch_array($result)) { // go through each row that was returned in $result
echo $table[0];
if($table[0] == $eventTitle)
{
echo "dont add";
}
else
{
echo"add";
}
}
/*$sql="INSERT INTO `assignment`.`attend` (`User`, `Event`) VALUES ('$user', '$eventTitle')";
$res=mysql_query($sql);
if($res)
{
Echo "Record successfully inserted";
//header("location: myTickets.php");
}
Else
{
Echo "There is some problem in inserting record";
//echo $name;
}
我尝试循环遍历“事件”行以查看其中的值是否等于所选的值。但由于它是一个 while 循环,因此它将继续循环。
伪代码明智
if(the value selected is == the value in the table && and the name is == to data in the User row then do not add, else add;
最佳答案
不要在 PHP 中执行此操作。相反,请在 user
和 event
上创建一个 UNIQUE 键(请参阅 here 如何执行此操作)。因此,这是两列上的一个键。这意味着这两列中的任何一对值只能出现一次。但是,用户可以参加多个事件,并且事件可以由多个用户参加。
这样你就可以让你的 PHP 变得更小。
此外,这个 while 循环不会一直循环:当不再有结果时它就会结束。
另一个想法,更多地从用户体验的角度来看,是让已经订阅事件的用户无法再次订阅 - 例如,将订阅按钮更改为取消订阅按钮,或者完全删除它。但是,请始终确保要开始调整请求的用户仍无法再次订阅 - 例如,通过添加 UNIQUE key 。
关于php - 阻止用户两次报名参加事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28014821/