我制作了这种点数系统,用户可以花点积分。用户点数会被扣除。我没有包含很多变量,但它们都没问题。问题出现在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);
而不是返回,但我希望用户能够购买多个项目(添加它而不是返回确实有效)。
答案 0 :(得分: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,这将导致更多问题。上面做出的更改(在第二个示例中),但此代码仍然不是我称之为最佳实践。
答案 1 :(得分:0)
我同意@leftclickben上面所说的一切,但是想再添加一个观察。我似乎在您致电$error_message
之后的代码中使用$success_message
和product()
的值,但为了让功能更改它们的价值,您必须通过参考,使用&
符号。
将您的函数原型更改为
function product($price,$points, $name, $uname, &$error_message, &$success_message) {
,函数返回后,值的变化将可用。