检查PHP / MYSQL表中是否已有值

时间:2013-01-10 16:21:15

标签: php mysql sql

我想检查表中是否已有值。 我桌子的结构是这样的:
ApplicantId = INT
EventId = INT
StudentId = INT

无需使用unique,因为这些表具有依赖关系。

以下是我到目前为止所尝试的内容:

include('../connectdb.php');

$ScholarPointId = $_GET["ScholarPointId"];
$Point = $_GET["Point"];
$ScholarId = $_GET["ScholarId"];
$EventId = $_GET["EventId"];

$verifysql = mysql_query("SELECT EventId FROM scholar_attended_events WHERE ScholarId ='$ScholarId' ");

#$resultVerify = mysql_fetch_assoc($verifysql);
$num_rows = mysql_num_rows($verifysql);

if( $num_rows > 0 )
{
 $script = "<script>
     alert('The user has already attended this event!');
            </script>";
 header('updatescholarpoints.php');
 exit();
}
else{
$result = mysql_query("UPDATE scholar_points 
SET scholar_points.Points = 
scholar_points.Points + $Point  
WHERE scholar_points.ScholarPointId = '$ScholarPointId' ") or die(mysql_error());

mysql_query("INSERT INTO scholar_attended_events (EventId , ScholarId) VALUES( '$EventId' , '$ScholarId' ) ") 
or die(mysql_error());  

}

?>

我想要的是检查Student = StudentId是否已经使用了EventId。如果是,则系统将提示警告框。否则,更新并插入相应的表中。我怎样才能做到这一点?我好像在这里想念一些东西。如果你能提供帮助,我真的很感激。

1 个答案:

答案 0 :(得分:1)

错过了=

$verifysql = mysql_query("SELECT EventId FROM scholar_attended_events WHERE ScholarId =$ScholarId ");

(并使用PDO或mysqli,您的代码实际上处于弃用状态)