Php 执行连接到数据库和执行数据库查询时出错
我正在为网站创建登录。php文件通过检查用户详细信息来控制显示内容。如果用户在登录表单中登录,则会显示一条消息。我已经包含了connect.php和core.php文件。当我打开index.php时,我得到以下错误。变量con已定义,但它表明它未定义请帮助我找到错误,谢谢提前 错误:Php 执行连接到数据库和执行数据库查询时出错,php,mysql,Php,Mysql,我正在为网站创建登录。php文件通过检查用户详细信息来控制显示内容。如果用户在登录表单中登录,则会显示一条消息。我已经包含了connect.php和core.php文件。当我打开index.php时,我得到以下错误。变量con已定义,但它表明它未定义请帮助我找到错误,谢谢提前 错误: Notice: Undefined variable: con in C:\xampp\htdocs\includes\core.php on line 33 Warning: mysqli_query() ex
Notice: Undefined variable: con in C:\xampp\htdocs\includes\core.php on line 33
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\includes\core.php on line 33
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\includes\core.php on line 34
index.php
<?php
require_once 'includes/connect.php' ;
require_once 'includes/core.php' ;
if(logged_in())
{
echo "You are logged in" ;
}
else
{
?>
<form action="login.php" method="post">
<table>
<tr>
<td>Email</td>
<td>Password <a href="resetpass.php">Forgot Password?</a></td>
<td></td>
</tr>
<tr>
<td><input type="email" name="sin_email" placeholder="Email" name="email"></td>
<td><input type="password" name="sin_pass" placeholder="Password" name="pass"></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form>
<?php
}
?>
<?php
//Start output buffer.Stores all the output of the server into a stack
ob_start() ;
//Start a session
session_start() ;
//Get the name of current file
$current_file=$_SERVER['SCRIPT_NAME'] ;
//Get the referrer to this page if any
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']))
{
$http_referrer=$_SERVER['HTTP_REFERER'] ;
}
function logged_in()
{
if(isset($_SESSION['login_token'],$_SESSION['user_name'],$_SESSION['user_id'])
&& !empty($_SESSION['login_token']) && !empty($_SESSION['user_name']) && !empty($_SESSION['user_id']))
{
//Store session variable to local variables
$user_name=$_SESSION['user_name'] ;
$user_id=$_SESSION['user_id'] ;
$http_agent=$_SERVER['HTTP_USER_AGENT'] ;
$ip_address=$_SERVER['REMOTE_ADDR'] ;
//Recreating the token string
$token_string=$user_name.$user_id.$http_agent.$ip_address ;
//Obtaining the md5 value for the token_string
$obtained_login_token=md5($token_string) ;
//Fetching previous token_string stored in the database
$token_query="SELECT * FROM securelogin WHERE user_id='$user_id' ;" ;
$token_result=mysqli_query($con,$token_query) ;
$token_info=mysqli_fetch_assoc($token_result) ;
$stored_login_token=$token_info['login_string'] ;
if($stored_login_token===$obtained_login_token)
{
return true ;
}
else
{
return false ;
}
}
else
{
return false ;
}
}
?>
<?php
$host_name="localhost" ;
$user_name="root" ;
$password="" ;
$db_name="futsided" ;
$con=mysqli_connect($host_name,$user_name,$password,$db_name)
or
die("Cannot connect to our server :(<br/>Please try again later or contact <a href=\"/report.html\">Administrator</a>") ;
?>
电子邮件
暗语
包括/core.php
<?php
require_once 'includes/connect.php' ;
require_once 'includes/core.php' ;
if(logged_in())
{
echo "You are logged in" ;
}
else
{
?>
<form action="login.php" method="post">
<table>
<tr>
<td>Email</td>
<td>Password <a href="resetpass.php">Forgot Password?</a></td>
<td></td>
</tr>
<tr>
<td><input type="email" name="sin_email" placeholder="Email" name="email"></td>
<td><input type="password" name="sin_pass" placeholder="Password" name="pass"></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form>
<?php
}
?>
<?php
//Start output buffer.Stores all the output of the server into a stack
ob_start() ;
//Start a session
session_start() ;
//Get the name of current file
$current_file=$_SERVER['SCRIPT_NAME'] ;
//Get the referrer to this page if any
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']))
{
$http_referrer=$_SERVER['HTTP_REFERER'] ;
}
function logged_in()
{
if(isset($_SESSION['login_token'],$_SESSION['user_name'],$_SESSION['user_id'])
&& !empty($_SESSION['login_token']) && !empty($_SESSION['user_name']) && !empty($_SESSION['user_id']))
{
//Store session variable to local variables
$user_name=$_SESSION['user_name'] ;
$user_id=$_SESSION['user_id'] ;
$http_agent=$_SERVER['HTTP_USER_AGENT'] ;
$ip_address=$_SERVER['REMOTE_ADDR'] ;
//Recreating the token string
$token_string=$user_name.$user_id.$http_agent.$ip_address ;
//Obtaining the md5 value for the token_string
$obtained_login_token=md5($token_string) ;
//Fetching previous token_string stored in the database
$token_query="SELECT * FROM securelogin WHERE user_id='$user_id' ;" ;
$token_result=mysqli_query($con,$token_query) ;
$token_info=mysqli_fetch_assoc($token_result) ;
$stored_login_token=$token_info['login_string'] ;
if($stored_login_token===$obtained_login_token)
{
return true ;
}
else
{
return false ;
}
}
else
{
return false ;
}
}
?>
<?php
$host_name="localhost" ;
$user_name="root" ;
$password="" ;
$db_name="futsided" ;
$con=mysqli_connect($host_name,$user_name,$password,$db_name)
or
die("Cannot connect to our server :(<br/>Please try again later or contact <a href=\"/report.html\">Administrator</a>") ;
?>
包括/connect.php
<?php
require_once 'includes/connect.php' ;
require_once 'includes/core.php' ;
if(logged_in())
{
echo "You are logged in" ;
}
else
{
?>
<form action="login.php" method="post">
<table>
<tr>
<td>Email</td>
<td>Password <a href="resetpass.php">Forgot Password?</a></td>
<td></td>
</tr>
<tr>
<td><input type="email" name="sin_email" placeholder="Email" name="email"></td>
<td><input type="password" name="sin_pass" placeholder="Password" name="pass"></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form>
<?php
}
?>
<?php
//Start output buffer.Stores all the output of the server into a stack
ob_start() ;
//Start a session
session_start() ;
//Get the name of current file
$current_file=$_SERVER['SCRIPT_NAME'] ;
//Get the referrer to this page if any
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']))
{
$http_referrer=$_SERVER['HTTP_REFERER'] ;
}
function logged_in()
{
if(isset($_SESSION['login_token'],$_SESSION['user_name'],$_SESSION['user_id'])
&& !empty($_SESSION['login_token']) && !empty($_SESSION['user_name']) && !empty($_SESSION['user_id']))
{
//Store session variable to local variables
$user_name=$_SESSION['user_name'] ;
$user_id=$_SESSION['user_id'] ;
$http_agent=$_SERVER['HTTP_USER_AGENT'] ;
$ip_address=$_SERVER['REMOTE_ADDR'] ;
//Recreating the token string
$token_string=$user_name.$user_id.$http_agent.$ip_address ;
//Obtaining the md5 value for the token_string
$obtained_login_token=md5($token_string) ;
//Fetching previous token_string stored in the database
$token_query="SELECT * FROM securelogin WHERE user_id='$user_id' ;" ;
$token_result=mysqli_query($con,$token_query) ;
$token_info=mysqli_fetch_assoc($token_result) ;
$stored_login_token=$token_info['login_string'] ;
if($stored_login_token===$obtained_login_token)
{
return true ;
}
else
{
return false ;
}
}
else
{
return false ;
}
}
?>
<?php
$host_name="localhost" ;
$user_name="root" ;
$password="" ;
$db_name="futsided" ;
$con=mysqli_connect($host_name,$user_name,$password,$db_name)
or
die("Cannot connect to our server :(<br/>Please try again later or contact <a href=\"/report.html\">Administrator</a>") ;
?>
必须将变量$con传输到已登录的函数($con未全局定义): 在index.php中更改
if(logged_in())
function logged_in()
到
在includes/core.php中更改
if(logged_in())
function logged_in()
到
它说,
$con
没有定义,您所展示的代码中没有一个定义了它,所以我打赌PHP是对的,而您是错的。在将其显示为showconnect.PHP
code之前,请尝试至少在谷歌上搜索一次该项目。您在第33行使用$con,但之前没有定义它,您需要使用mysqli_连接并将其关联到var