循环,比较多个表值

时间:2015-03-02 22:26:26

标签: php mysql database foreach registration

我一直有问题&不确定我需要的逻辑。

我正在尝试构建一个基本的PHP& MySQL注册页面虽然检查了他们是否已更改IP地址,但检查地址是否已注册3个不同的帐户,如果是,则返回false。

这是我的逻辑。

帐户注册 - >数据库(site_registration) - >已验证电子邮件 - >数据库(用户) - >帐户登录 - >数据库(account_logins)(仅限新IP)

帐户尝试在3个条目后重新注册 - >检查数据库(site_registration)IP字段 - >检查数据库(account_logins)帐户IP字段 - >将该帐户的所有IP都放入一个数组中 - >根据site_registration检查数组 - >如果在3个帐户中找到IP,则抛出注册错误 - >数据库(suspicious_logs)

这是我需要的mysql代码,但不知道如何循环它。

//SELECT * FROM users WHERE (idnumber = '75.143.xxx.xxx') OR (idnumber = '76.94.xxx.xxx') OR (idnumber = '76.94.xxx.xxx')

$username = $_SESSION['login'];
$check_ip_site = $MySQL->consult("SELECT * FROM site_registration WHERE (username = '$username')");
$check_ip_logins = $MySQL->consult("SELECT * FROM site_logins WHERE (username = '$username')");
$check_ip_user = $MySQL->consult("SELECT * FROM users");

for($i = 0; $login_array[$i] = mysql_fetch_assoc($check_ip_logins); $i++);
    array_pop($login_array);

for($i = 0; $user_array[$i] = mysql_fetch_assoc($check_ip_user); $i++);
    array_pop($user_array);

foreach ($login_array["ip"] as $login_ips) {
    if (in_array($login_ips, $user_array["ip"]) > 3) {
        return true;
    } else { 
        return false;
    }
}

虽然这种语法不起作用,但这就是我需要的,我想知道如果在用户数据库中的3条记录中找到任何IP,如果它们返回false。

1 个答案:

答案 0 :(得分:0)

这对我有用。我不得不将它设置为foreach,以便循环查询。另外,我的for循环就像那样,因为我在循环内部无所事事。如果有人看到更好的方法,请告诉我!在我接受我的回答之前,我会看看是否有人有更好的解决方案。

//Check if username is set, if it's not use the current IP instead (add cookie handling later to keep from destroying evidence)
$ip = $_SERVER['REMOTE_ADDR'];
if ($username == NULL){
    $check_ip_logins = $MySQL->consult("SELECT * FROM site_logins WHERE (ip = '$ip')");
}
else
{
    $check_ip_logins = $MySQL->consult("SELECT * FROM site_logins WHERE (username = '$username')");
}

for($i = 0; $login_array[$i] = mysql_fetch_assoc($check_ip_logins); $i++);
array_pop($login_array); //Deletes the null array at the end of the for loop

foreach ($login_array as $la)
{
    $lips = $la["ip"]; //Place just the IP's into it's own array

    $check_ip_user = $MySQL->consult("SELECT * FROM users WHERE (idnumber = '$lips')"); //Loop through all IP's associated with account & check the users database

    while($test = mysql_fetch_assoc($check_ip_user)) {
        $names[] = $test["name"]; // This is used as a way to count, any thing can be used here.
    }
}
if (count($names) > 3)
{
    echo 'you have too many accounts you cannot create anymore';
}
else
{
    echo 'you are able to create an account';
}