检查两个表是否具有相同的值(并添加复选标记)

时间:2012-03-06 09:38:08

标签: php mysql database if-statement

我想执行以下操作:检查用户是否已在第二个表中。如果用户是,我收回回显:“用户在数据库中”,当用户在数据库中或不在数据库中时,它还应该向表中添加值。截至目前,它似乎做了正确的事情,但没有改变用户旁边的价值。

<?php

$DB_HostName = "localhost";
$DB_Name = "db";
$DB_User = "user";
$DB_Pass = "pw";
$DB_Table = "connection";
$DB_Table2 = "contacts";

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die (mysql_error()); 
mysql_select_db($DB_Name,$con) or die(mysql_error()); 

$query = "SELECT User FROM $DB_Table left join $DB_Table2 on User = Number";

if ($result) {

    if (mysql_num_rows($result) > 0) {

echo "This user is already in database";
mysql_query("INSERT INTO $DB_Table2 (Answer)
VALUES ('http://myserver/thumbsup.png')");        
}
else {
echo "This user isn't in the database";
mysql_query("INSERT INTO $DB_Table2 (Answer)
VALUES ('http://myserver/thumbsdown.png')");    
}
}
mysql_close($con);

?>

1 个答案:

答案 0 :(得分:1)

在查看代码时,很难看到您在这里尝试做什么。无论如何,我确实认为您需要从表格中查看哪个用户。

$query = "SELECT User FROM $table_id left join $table_id2 on User = Number";

应该更像

$query = "SELECT User FROM $DB_Table left join $DB_Table2 on User = Number where $DB_Table.User = " . $_SESSION['userid'];

如果您以某种方式对用户进行了身份验证,并且用户ID号位于名为“userid”的会话变量中。你显然没有想到这一点,因为你在第二个表的链接中添加了每个其他键的默认值。