0

我制作了这种积分系统,用户可以在其中使用积分。用户积分确实会被扣除。我没有包括很多变量,但它们都还可以。问题发生在return($success)return($error_message)

这是代码:

function died($error) {
    header("Location: error_points_on.php?error=" . $error);
    die();
}

function success($success) {
    header("Location: success_points_on.php?success=" . $success);
    die();
}
function quote_smart($value, $handle) {

if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}

if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value, $handle) . "'";
}
return $value;
}

function product($price,$points, $name, $uname, $error_message, $success_message) {


    $user_name = "cencord";
    $pass_word = "cencord";
    $database = "cencord";
    $server = "cencord";

    $db_handle = mysql_connect($server, $user_name, $pass_word);
    $db_found = mysql_select_db($database, $db_handle);

 if ($db_found) { // connect to DB

            $uname = quote_smart($uname, $db_handle);
            $SQL = mysql_query("SELECT points FROM members WHERE username=$uname");
            $points = mysql_fetch_row($SQL);

            $points = $points[0]; // make it a variable rather then an array


    if ($points >= $price) {

         $points = $points-$price; // fjern points

         $points = quote_smart($points, $db_handle);

         mysql_query("UPDATE members SET points=$points WHERE username = $uname");

         $success_message .= "The " . $name . " has been mailed to your E-mail, please allow 5 minutes for it to arrive.<br />";
         return($success_message);
    }


        else if ($points < $price) {

        $error_message .= "You have " . $points . " you need " . $price . " points to purchase a " . $name;
        return($error_message);
        }

else if (!$db_found) {
$error_message .= "Could not connect to the database, please contact support";
return($error_message);
    }
}
}



  if($Checked1 == true) {
      product(400, $points, "Some string", $uname,     $error_message, $success_message); //price and name
}
  if($Checked2 == true) {

      product(400, $points, "Some string", $uname,     $error_message, $success_message);
}



 if(strlen($error_message) > 0) {
    died($error_message);
  }
 if(strlen($success_message) > 0) {
    success($success_message);
  }

echo "error didnt pass at all";

我可以添加

header("Location: success_points_on.php?success=" . $success); 

而不是退货,但我希望用户能够购买多件商品(添加它而不是退货确实有效)。

4

2 回答 2

0

我同意@leftclickben 上面所说的一切,但想再补充一点。我似乎在调用 之后的代码中使用了$error_message和的值,但是要让函数改变它们的值,你必须通过引用传递它们,使用符号。$success_messageproduct()&

将您的函数原型更改为

function product($price,$points, $name, $uname, &$error_message, &$success_message) {

并且值的更改将在函数返回后可用。

于 2013-02-02T17:52:15.400 回答
0

你的逻辑嵌套是错误的。去掉胆量,只留下 if 语句,你的product()函数中有:

function product($price,$points, $name, $uname, $error_message, $success_message) {
    if ($db_found) { // connect to DB
        if ($points >= $price) {
            $success_message = "blah";
            return($success_message);
        } 
        else if ($points < $price) {  // This if () part is redundant, btw
            $error_message = "blah";
            return($error_message);
        }
        else if (!$db_found) {
            $error_message .= "blah"; // This should be = instead of .=
            return($error_message);
        }
    }
}

你想要的是:

function product($price,$points, $name, $uname, &$error_message, &$success_message) {
    if ($db_found) { 
        if ($points >= $price) {
            $success_message = "blah";
            return($success_message); // this is redundant actually
        } else {
            $error_message = "blah";
            return($error_message);  // this is redundant actually
        }
    } else {
        $error_message = "blah";
        return($error_message);  // this is redundant actually
    }
}

我强烈建议使用一些工具,例如 IDE,它可以使您的代码保持正确格式,这将使这些类型的问题更容易看到。

编辑

我也刚刚注意到您没有通过引用传递 $error_message 和 $success_message ,这将导致进一步的问题。上面所做的更改(在第二个示例中)但此代码仍然不是我所说的最佳实践。

于 2013-02-02T17:47:31.757 回答