此代码的一部分有效,但其余部分无效

时间:2013-07-09 20:58:04

标签: php mysql

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

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不会更新。他们都去“Toon name / Guild更新为:WHATEVER”,但他们实际上没有写到桌子上。

1 个答案:

答案 0 :(得分: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_函数;我绝对建议彻底测试,以确保我没有改变任何地方的逻辑。