Cleaning up PHP, and cleaning up unnecessary code

Asked
Viewd346

0

I'm still new to certain PHP functions. Is there any way I can clean up the following code, because I know all of this is just unnecessary, and it's giving me a headache. Everything after the if statement is the same for each set of code.

    if($class == "2"){if ($posts >= 1){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25  WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}}


if($class == "3"){if ($posts >= 2){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}


if($class == "4"){if ($posts >= 3){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}}


if($class == "5"){if ($posts >= 4){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}}


if($class == "6"){if ($posts >= 5){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}}


if($class == "7"){if ($posts >= 6){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else { $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error()); }}}}}}

6 个答案

4

您不是从您先前的问题中学到了什么吗?

 if ($class >= 2 && $class <= 7) {
    if ($posts >= ($class - 1)) {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25  WHERE `users`.`friendid`='$friend'";
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
        if (!mysql_query($sql)) {
            die('Error: ' . mysql_error());
        }
    } else {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
        mysql_query($sql) or die(mysql_error());
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
    }
}
 
  • @Joey: But the pattern was almost the same. So you first could have tried it on your own.

    GumboSeptember 07, 2009 09:00
0
 <?php

function update_users($friend){

$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";  

if (!mysql_query($sql)) {  die('Error: ' . mysql_error());    } 

}

function insertproof($friend,$binfo){

$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";

mysql_query($insert) or die(mysql_error());    

echo "";

}

?> 
 

认为这可能对您有用。

1

如果那样简单-if语句后的所有代码集都相同-您可以简单地创建函数并为每个类调用它。

仔细阅读这段代码也让我头疼,所以我没有在整个过程中都这样做,但是看起来这里可以进行一些重大的重构。

我一眼就能知道的-您不需要所有这些if语句,因为它们始终适合模式:

 if ($posts >= ($class - 1)) {
    ...
}
 
-1

这有点令人眼花asia乱,并且可以使用,您在其中删除了一些}

我至少会将一些重复的代码分解为几个函数

 <?php

if ($class == "2") {
    if ($posts >= 1) {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25  WHERE `users`.`friendid`='$friend'";
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
        if (!mysql_query($sql)) {
            die('Error: ' . mysql_error());
        }
        echo "";
    } else {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
        mysql_query($sql) or die(mysql_error());
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
    }
}

if ($class == "3") {
    if ($posts >= 2) {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
        if (!mysql_query($sql)) {
            die('Error: ' . mysql_error());
        }
        echo "";
    } else {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
        mysql_query($sql) or die(mysql_error());
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
    }
    if ($class == "4") {
        if ($posts >= 3) {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
            if (!mysql_query($sql)) {
                die('Error: ' . mysql_error());
            }
            echo "";
        } else {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
            mysql_query($sql) or die(mysql_error());
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
        }
    }
    if ($class == "5") {
        if ($posts >= 4) {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
            if (!mysql_query($sql)) {
                die('Error: ' . mysql_error());
            }
            echo "";
        } else {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
            mysql_query($sql) or die(mysql_error());
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
        }
    }
    if ($class == "6") {
        if ($posts >= 5) {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
            if (!mysql_query($sql)) {
                die('Error: ' . mysql_error());
            }
            echo "";
        } else {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
            mysql_query($sql) or die(mysql_error());
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
        }
    }
    if ($class == "7") {
        if ($posts >= 6) {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
            if (!mysql_query($sql)) {
                die('Error: ' . mysql_error());
            }
            echo "";
        } else {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
            mysql_query($sql) or die(mysql_error());
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
        }
    }
}

?>
 
3

像我一样疼的眼睛。这里有一些提示。

0-编写供人类阅读的代码。要学习的最重要的一课是代码是供人类阅读而不是计算机。

1-代码样式。注意花括号。通常,{应该使{和随附的}之间的所有代码缩进。 4个空格是缩进的合理数量。这使您的代码意图更容易确定,因此更容易发现错误。

2-验证所有输入。永远不要信任用户提交的任何内容。在您的sql语句中,您具有以下内容:

 $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25  WHERE `users`.`friendid`='$friend'";`
 

问题是$friend变量。我认为这是基于用户输入的。用户可能会输入讨厌的东西,例如2; DELETE * FROM *(好吧,SQL可能不正确,但您明白了)。验证所有输入,并始终使用SQL参数。

3-使用功能。应该使用函数来减少代码重复。减少代码中的语句数量,减少了潜在的错误。应该使用函数来指示代码的意图。例如,如果您有一个复杂的表达式用于if语句,则可以将表达式移到单独的函数中。例如if (isDateInRange($date, $range)) {}

最后,花一些时间来思考您的代码。问问自己“代码的意图是否明确?”,“是否有更好的方法呢?”。确保您了解代码的每一行。刚开始时的诱惑是要保持态度,“万岁,行得通!让我们继续前进'。抵制这种诱惑,从长远来看不会很好地为您服务。

  • @Joey. My point was that you should always validate input. Always use SQL parameters, regardless of where the data comes from.

    Benedict CohenSeptember 07, 2009 10:17
  • $friend = $_SESSION[‘userid’]; located in my config file.

    homeworkSeptember 07, 2009 09:45
1

我从中提取了两个函数insert()update()。这是insert()的代码,我将另一个留给您:

 /**
 * @param  string $table  Table name to insert $values into
 * @param  array  $values Key is field name, value is field value to insert
 * @return null
 * @throws Exception if query fails
 */
function insert($table, $values) {
    $sql = array();

    foreach ($values as $field => $value) {
        $sql[] = "`$field` = '" . mysql_real_escape_string($value) . "'";
    }

    $sql = "INSERT INTO `$table` SET " . implode(', ', $sql);

    if (! mysql_query($q)) {
        throw new Exception(mysql_error());
    }
}
 

完成此步骤后,使用它,看看还有什么进一步的代码重复并将其提取到其他功能中。

对于update(),您需要为WHERE零件添加第三个参数。