嗯,我在这里看到了很多错误...
首先你的查询应该被清理...
$email = mysql_real_escape_string ($_POST['email']); // escape the email
$pass = SHA1(mysql_real_escape_string ($_POST['pass'])); // escape and encrypt the pass
// now you can put it into the query safely
$query = "SELECT user_id from toddprod where email = '$email' and pass = '$pass' ";
接下来你执行错误的查询,该mysql_query函数有两个参数,查询和数据库连接。你传递了错误的参数,你传递了查询和mysql_select_db函数的结果,它只是一个布尔值。因此,您$dbc不必$db进入该查询,即使那样您也以错误的顺序传递参数。查询首先进行,然后是连接。所以应该是...
$result = mysql_query($query, $dbc);
接下来,您尝试将mysql_query函数的返回值设置为您的 cookie,但该值是一种资源,而不是您需要的用户 ID。您必须像这样从资源中实际读取值。
$row = mysql_fetch_array($result);
$userid = $row["user_id"];
setcookie('user_id', $userid);
继续...当您设置电子邮件 cookie 时,您将变量放在单引号中,因此 cookie 实际上将包含$e而不是实际的电子邮件,因为单引号存储字符串litterly(不解析变量)。因此,您应该使用双引号,或者根本不使用引号。因此,以下任何一项都可以...
setcookie('email', "$e");
setcookie('email', $e);
最后但并非最不重要的一点是,您的 if 语句末尾不应有分号,并且您再次需要将连接而不是数据库选择结果传递给mysql_close函数,所以它应该是
mysql_close($dbc);
在那里,希望这能让您有所收获,尝试这些更改,如果问题仍然存在,我很乐意为您提供进一步的帮助。
以下是可以帮助您的链接:
http://www.php.net/manual/en/function.mysql-query.php
http://www.php.net/manual/en/function.mysql-fetch-array.php
http://www.php.net/manual/en/function.mysql-real-escape-string.php
编辑:
在这里,我根据我发现的问题修复了代码。试试看,我无法测试它,所以它可能会出现一些小的语法错误,但它应该给你一些可比较的东西。同样对于未来,我建议您以语义/正确的方式命名您的变量,以便其他人更容易获取,并且它还可以防止您感到困惑,就像您将 $db 而不是 $dbc 传递给您的一些函数一样。
<?php
// keep the function names in lowercase, no reason, just looks better to me
define('DB_USER', 'usernamegoeshere');
define('DB_PASSWORD', 'passwordhere');
define('DB_HOST', 'hostnamehere');
define('DB_NAME', 'andtheotherthinghere');
// connect to the mysql server
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL');
// select the database, you don't need to store the result, it just returns true or false
mysql_select_db(DB_NAME, $conn) or die('Could not select database.' .mysql_error());
// escape the input
$email = mysql_real_escape_string($_POST['email']);
$pass = sha1(mysql_real_escape_string($_POST['pass']));
// create the query
$query = "SELECT user_id FROM toddprod WHERE email = '$email' AND pass = '$pass'";
// execute the query
$result = mysql_query($query, $conn);
$usercount = mysql_num_rows($result);
if($usercount == 1){
// read the results and get the user_id
$row = mysql_fetch_array($result);
$userid = $row['user_id'];
// set the cookies
setcookie('user_id', $userid);
setcookie('email', $email);
setcookie('logged-in', 'true');
// echo success message
echo 'good';
}elseif($usercount == 0) {
echo "You're $email with password $pass";
}
mysql_close($conn);
?>