SQL:Double if语句/数据库查询问题

时间:2014-02-03 21:08:04

标签: php mysql sql

我是SQL和PHP编码的新手,但我正在尝试执行以下操作:

为了防止登录用户两次参加比赛,我将他们的用户ID和竞争ID( comp )记录到一个名为竞赛的SQL表中。当用户加载页面时,我希望能够查询数据库以检查该用户标识是否已进入特定竞争对手。如果有,则会显示该页面的一个版本,如果它们没有显示第二个版本。

基本上,我正在查看 userid == x AND comp == y 是否在表格的给定行中。

这是我到目前为止所获得的代码,它似乎并没有起作用。

$result = databaseSelect("select id, userid, comp from competitions where userid=" . $_SESSION['user']);

 if (count($result) > 0) {
 $r2 = ("select" . $result['id'] . " from" . $result . " where" . $result['comp'] . " == " . $compid);
 if (count($r2) > 0) {
 echo "<p>No competitions</p>";
 }
 else{

 echo "<p>Competition...</p>";
 }

2 个答案:

答案 0 :(得分:1)

您可以查看用户是否已输入ID为$ comp_id的特定比赛。如果有结果则表示用户已参加比赛。

$result = databaseSelect("select id, userid, comp from competitions where userid=" . $_SESSION['user'] . " AND comp=" . $comp_id );

if (count($result) > 0) {
    echo "You have already entered this competition";
} 
else {
    echo "This is the competition...";
}

答案 1 :(得分:0)

尝试此查询:

//This will return a row if $_SESSION['user'] already has a competition with $compid 
$result = databaseSelect("select id, userid, comp from competitions where userid=" . $_SESSION['user'] ." AND comp = ". $compid);
if (count($result) > 0) {
  // user already partecipates to the competition 
}
else{
  //ok, you can partecipate
}