每当我尝试登录我的网站时,它总是会发出:
注意:未定义的变量:第13行的P:\ xampp \ htdocs \ CraftedLogin \ authenticate.php中的conn
警告:mysql_query()期望参数2为resource,给定null 在第13行的P:\ xampp \ htdocs \ CraftedLogin \ authenticate.php
警告:mysql_num_rows()期望参数1为resource,null 在第16行的P:\ xampp \ htdocs \ CraftedLogin \ authenticate.php中给出 错误的用户名或密码!无法以gfg身份登录
这是我的代码:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
include("dbConfig.php");
$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'";
$rs = mysql_query($sql,$conn);
$num = mysql_num_rows( $rs );
if( $num != 0 )
{
$msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
}
else
{
$msg = "Wrong username or password! Cannot login as $username";
}
?>
<html>
<head>
<title>Log-In</title>
</head>
<body>
<?php echo( $msg ); ?>
</body>
</html>
这也是我的dbConfig.php:
<?
$host = "localhost";
$user = "CraftedLogin";
$pass = "craftedlogin";
$db = "craftedlogin";
$self = $_SERVER['PHP_SELF'];
$referer = $_SERVER['HTTP_REFERER'];
$connection = mysql_connect($host, $user, $pass);
if ( !$conn )
{
echo "Error connecting to database.\n";
}
$rs = @mysql_select_db( $db, $conn )
or die( "Could not select database" );
?>
任何人都可以帮助我吗?
注意:我已经尝试将$ connection更改为$ conn但是这次仍然使用$ num变量发出错误,但我想我可以快速解决这个问题。
新代码:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
include("dbConfig.php");
$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'";
$rs = @mysql_select_db( $db, $conn );
$numr = mysql_num_rows( $rs );
if( $numr != 0 )
{
$msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
}
else
{
$msg = "Wrong username or password! Cannot login as $username";
}
?>
<html>
<head>
<title>Log-In</title>
</head>
<body>
<?php echo( $msg ); ?>
</body>
</html>
和dbConfig.php:
<?
$host = "localhost";
$user = "CraftedLogin";
$pass = "craftedlogin";
$db = "craftedlogin";
$self = $_SERVER['PHP_SELF'];
$referer = $_SERVER['HTTP_REFERER'];
$conn = mysql_connect($host, $user, $pass);
if ( !$conn )
{
echo "Error connecting to database.\n";
}
$rs = @mysql_select_db( $db, $conn )
or die( "Could not select database" );
?>
答案 0 :(得分:0)
当您在其他地方使用$connection
时,您的联系为$conn
。
答案 1 :(得分:0)
更改
$connection = mysql_connect($host, $user, $pass);
到
$conn = mysql_connect($host, $user, $pass);
这就是为什么它不起作用。你使用了错误的变量名。
编辑: 这可能就是你想要的:
$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'";
$result = mysql_query( $sql, $conn );
$numr = mysql_num_rows( $result );
答案 2 :(得分:0)
在这段代码中:
$rs = @mysql_select_db( $db, $conn ) or die( "Could not select database" );
您正在传递$ conn,但您的连接被定义为$ connection。将其更改为:
$rs = @mysql_select_db( $db, $connection) or die( "Could not select database" );
将解决此问题。
答案 3 :(得分:0)
修复:
$conn = mysql_connect($host, $user, $pass);
答案 4 :(得分:0)
$connection = mysql_connect($host, $user, $pass);
更改为
$ conn = mysql_connect($ host,$ user,$ pass);