繁体   English   中英

检查表PHP / MYSQL中是否已存在值

[英]Check if a value is already in the table PHP/MYSQL

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

无需使用唯一,因为这些表具有依赖性。

以下是到目前为止我尝试过的操作:

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());  

}

?>

我要检查的是EventId是否已由Student = StudentId接受。 如果是这样,则系统将提示一个警告框。 否则,请更新并插入到相应的表中。 我怎样才能做到这一点? 看来我想念这里的东西。 如果可以的话,我非常感谢。

只是缺少一个=

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

(并使用PDO或mysqli,您的代码实际上处于不推荐使用的模式)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM