我目前正致力于构建一个下载平台,其中用户收到一个随机代码并使用它来访问一个MP3下载多达三个下载。我使用Python生成了一个随机代码列表,并将它们导入到一个SQL表中,其中一个空列用于关联的电子邮件地址和一个默认为0的使用次数。我编写了下面的PHP脚本,以便将电子邮件与特定的代码相关联并添加到计数中,以便下载最多可以访问三次。PHP MySQL SELECT查询只返回资源
$email = $_POST["email"];
$email = stripslashes($email);
$uniqueCode = $_POST["uniqueCode"];
$uniqueCode = stripslashes($uniqueCode);
// check that all fields are filled
if($uniqueCode=="" || $email=="")
apologize("Please fill out all fields.");
// check to make sure that the e-mail is valid
if (verifyEmail($email) == FALSE)
apologize("Please enter a valid e-mail address.");
// check if uniqueCode input is alphanumeric
if (verifyCode($uniqueCode) == FALSE)
apologize("Download codes are alphanumeric.");
// check to see if unique code is correct
$sql = mysql_query("SELECT * FROM wd009 where uniqueCode='$uniqueCode'");
$result = mysql_fetch_array($sql);
if($sql==FALSE)
{
apologize("Your download code is invalid. Please try again");
}
// only allow users with less than 3 downloads to proceed
else if ($result['count'] <= 3) {
if ($result['email'] == ""){
mysql_query("UPDATE wd009 SET email='$email', count=1 WHERE uniqueCode='$uniqueCode'");
apologize("added email");
}
else if ($result['email'] != $email)
apologize("different email from record!!");
else if ($result['email'] == $email){
mysql_query("UPDATE wd009 SET count=count+1 WHERE uniqueCode='$uniqueCode'");
apologize("updated the count!");
}
else
apologize("Your download code is used up!");
很显然,我用在上面的一些功能未包含在代码中,但我已经检查过所有这些,没有人应与MySQL查询干扰。可能值得注意的是,道歉后()道歉后立即退出。当我在表单中输入正确的代码时,它可以正常工作并更新SQL数据库。但是,只要下载代码输入是字母数字,即使字符串绝对不匹配表中的任何字符,表单也会接受它。即,无论输入如何,mysql_query都会返回一个资源。我已经检查了数据库连接,但是由于在下载代码正确的情况下正确更新了表,这似乎不成问题。
我试过调试这种方式,我可以想到的每一个方式,我真的很迷惑。任何帮助你可以提供将不胜感激!
我想说如果你使用PDO,你会感觉更熟悉。它不再使用这些资源thingies。 – hakre