查找具有匹配值和更新字段的MySQL表

时间:2014-04-18 02:32:59

标签: php mysql

我创建了一个注册验证系统,用户会收到一封电子邮件,其中包含用于验证其帐户的链接。单击链接后,它将转到下面的PHP页面:

<?php
$passkey = trim(mysqli_real_escape_string($connection, $_GET['passkey']));

$query = "UPDATE pims SET com_code=NULL WHERE com_code='$passkey' UNION ALL UPDATE dms SET com_code=NULL WHERE com_code='$passkey' UNION ALL UPDATE users SET com_code='$passkey'";

$result = mysqli_query($connection, $query);
    if (!result) {
        die("Database query failed: " . mysqli_error($result));
    }

if (mysqli_num_rows($result) == 0) {
    echo '<div>Sorry. Something went wrong! Please contact the site admin at <a href="mailto:support@domain.com">support@domain.com</a> for assistance.</div>';

} else {
    echo '<div>Your account is now active. You may now <a href="login.php">Log in</a></div>';
}
?>

我想将com_code设置为NULL,但首先我需要通过匹配$ passkey变量在3个表中的1个(pims,dms和users)中找到用户。

这不起作用。有什么建议吗?


更新:

我修正了Sean指出的代码:

$query = "UPDATE pims SET com_code=NULL WHERE com_code='$passkey' 
UNION ALL UPDATE dms SET com_code=NULL WHERE com_code='$passkey' 
UNION ALL UPDATE users SET com_code=NULL WHERE com_code='$passkey'";

但是,它仍然没有用。


更新:

我再次更新了代码:

<?php
    $passkey = mysqli_real_escape_string($connection, $_GET['passkey']);

    $query = "UPDATE pims, dms, users SET pims.com_code=NULL, dms.com_code=NULL, users.com_code=NULL WHERE pims.com_code='$passkey' AND dms.com_code='$passkey' AND users.com_code='$passkey'";

    $result = mysqli_query($connection, $query);
        if (mysqli_affected_rows ($connection) > 0) {
            echo '<div>Your account is now active. You may now <a href="login.php">Log in</a></div>';
        } else {
            echo '<div>ERROR MESSAGE<br><br>Sorry, something went wrong!<br><br>Please contact the site admin at <a href="mailto:support@domain.com">support@domain.com</a> for assistance.</div>';
    }

    mysqli_close($connection);
?>

但出于某种原因,它只是赢得了连接。


更新:

所以,我决定采用这样的3个查询:

<?php
    $passkey = mysqli_real_escape_string($connection, $_GET['passkey']);

    $query = "UPDATE pims SET com_code=NULL WHERE com_code='{$passkey}'";

    $result = mysqli_query($connection, $query);
        if (mysqli_affected_rows ($connection) <= 0) {

            $query = "UPDATE dms SET com_code=NULL WHERE com_code='{$passkey}'";

            $result = mysqli_query($connection, $query);
                if (mysqli_affected_rows ($connection) <= 0) {

                    $query = "UPDATE users SET com_code=NULL WHERE com_code='{$passkey}'";

                    $result = mysqli_query($connection, $query);
                        if (mysqli_affected_rows ($connection) <= 0) {
                            echo '<div> ERROR MESSAGE<br><br>Sorry, something went wrong!<br><br>Please contact the site admin at <a href="mailto:support@domain.com">support@domain.com</a> for assistance.</div>';
                        } else {
                            echo '<div>Your account is now active. You may now <a href="login.php">Log in</a></div>';
                        }
                } else {
                    echo '<div>Your account is now active. You may now <a href="login.php">Log in</a></div>';
                }
        } else {
            echo '<div>Your account is now active. You may now <a href="login.php">Log in</a></div>';
        }

    mysqli_close($connection);
?>

它有效,但似乎很慢。任何改进建议都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

尝试在1个查询中完成所有操作,而不是执行UNION ALL

UPDATE 
 pims,dms,users
SET
 pims.com_code=NULL, dms.com_code=NULL, users.com_code=NULL
WHERE 
 pims.com_code='$passkey'
AND
 dms.com_code='$passkey'
AND
 users.com_code='$passkey'

SQL小提琴示例 - http://sqlfiddle.com/#!2/d0464d/1