-2

为什么这只有一部分有效?我在工作部分添加了评论:

session_start(); 
$_SESSION['views']=1;
$pageon="edited";
$button=" ";
$user=$_SESSION['username'];

$otoon = $_POST['otoon'];
$oserver = $_POST['oserver'];
$toonname = mysql_real_escape_string($_POST['toonname']);
$server = mysql_real_escape_string($_POST['server']);
$guild = mysql_real_escape_string($_POST['guild']);


echo $user . " " . $otoon . " " . $oserver . " " . $toonname . "<br />";

if (!empty($toonname)) {
$tql="UPDATE
        reguserstest AS rt
        INNER JOIN
            alt_toon AS att
        ON
            att.`toonname` = rt.`toonname`
        SET
            rt.`toonname` = CASE WHEN rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' THEN '$toonname' 
                ELSE rt.`toonname` END ,

            att.`toonname` = CASE WHEN att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' THEN '$toonname' 
                ELSE att.`toonname` END
        WHERE
        ( rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' )
        OR
        ( att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' )";
if (mysql_query($tql,$con))
        {
            echo "Toon name updated to: " . $toonname . "<br />";
            } else {
            echo "<h1>Error</h1>";
        }
}

//BEGINNING OF THE WORKING CODE!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (!empty($server)) {
$sql="UPDATE
        reguserstest AS rt
        INNER JOIN
            alt_toon AS att
        ON
            att.`server` = rt.`server`
        SET
            rt.`server` = CASE WHEN rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' THEN '$server' 
                ELSE rt.`server` END ,

            att.`server` = CASE WHEN att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' THEN '$server' 
                ELSE att.`server` END
        WHERE
        ( rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' )
        OR
        ( att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' )";
if (mysql_query($sql,$con))
        {
            echo "Server updated to: " . $server . "<br />";
            } else {
            mysql_error();
        }
}
//THE WORKING CODE ENDS HERE ---- WHY??????

if (!empty($guild)) {
$gql="UPDATE
        reguserstest AS rt
        INNER JOIN
            alt_toon AS att
        ON
            att.`guild` = rt.`guild`
        SET
            rt.`guild` = CASE WHEN rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' THEN '$guild' 
            ELSE rt.`guild` END ,

            att.`guild` = CASE WHEN att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' THEN '$guild' 
            ELSE att.`guild` END
        WHERE
        ( rt.`username`='$user' AND rt.`server`='$oserver' AND rt.`toonname`='$otoon' )
        OR
        ( att.`username`='$user' AND att.`server`='$oserver' AND att.`toonname`='$otoon' )";
if (mysql_query($gql,$con))
        {
            echo "Guild updated to: " . $guild . "<br />";
            } else {
            echo mysql_error();
        }
}

正如我所说,我可以让 $server 100% 的时间更新。但 $toonname 和 $guild 不会更新。他们都去了“卡通名称/公会更新为:WHATEVER”,但他们实际上并没有写在桌子上。

4

1 回答 1

0

您正在为每个条件运行单独的更新查询;我认为正在发生的事情是早期的干扰了后面的。我建议将它们合理化,并简化 SQL:

$userSQL = "UPDATE reguserstest SET ";
$altSQL = "UPDATE alt_toon SET ";

$toUpdate = array();

if (!empty($toonname)) {
    $toUpdate[] = "toonname='" . $toonname . "'";
}

if (!empty($server)) {
    $toUpdate[] = "server='" . $server . "'";
}

if (!empty($guild)) {
    $toUpdate[] = "guild='" . $guild . "'";
}

if (count($toUpdate)) {
    $userSQL .= implode(",", $toUpdate) . " WHERE `username`='$user' AND `server`='$oserver' AND `toonname`='$otoon'";
    $altSQL .= implode(",", $toUpdate) . " WHERE `username`='$user' AND `server`='$oserver' AND `toonname`='$otoon'";

    mysql_query($userSQL) or die ("User query failed: " . mysql_error());
    mysql_query($altSQL) or die ("Alt query failed: " . mysql_error());
}

我没有对此进行测试,但你应该明白这一点。

它将分别更新两个表,每个查询一个。它检查是否设置了每个字段,并将其添加到数组中;如果该数组为空,则无需更新。否则,它会运行两个查询,如果其中一个失败,它就会死掉。

要从评论中重复一遍 - 您应该考虑迁移到 mysqli_ 或 PDO 以确保安全,因为 mysql_ 函数已被弃用;我肯定会建议彻底测试,以确保我没有在任何地方更改逻辑。

于 2013-07-10T15:27:49.097 回答