gpt4 book ai didi

php - 如何解决登录页面的错误

转载 作者:行者123 更新时间:2023-11-29 08:04:59 25 4
gpt4 key购买 nike

我是 PHP 编码新手,我正在尝试制作登录页面,但出现了一个我不明白的错误。

login.php

<html>
<body>

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="login_dash.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>User Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>

login_dash.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>
<body>
<?php
//var_dump($_POST);
// Grab User submitted information
$user = $_POST["myusername"];
$pass = $_POST["mypassword"];

// Connect to the database
$con = mysql_connect("localhost","root","","jj");
// Make sure we connected succesfully
if(! $con)
{
die('Connection Failed'.mysql_error());
}

// Select the database to use
mysql_select_db("jj",$con);

$result = mysql_query("SELECT myusername, mypassword FROM login WHERE myusername = $user");
var_dump($result);
$row = mysql_fetch_array($result,$con);

if($row["myusername"]==$user && $row["mypassword"]==$pass)
echo"You are a validated user.";
else
echo"Sorry, your credentials are not valid, Please try again.";
?>
</body>
</html>

我遇到了以下我无法理解的错误

错误:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SRK\login_dash.php on line 27

最佳答案

此错误是因为查询返回 false,所以出现了问题。尝试一下

$result = mysql_query("SELECT myusername, mypassword FROM login WHERE myusername = '$user'");

PHP 5.5 中不推荐使用 mysql 函数,您应该使用 PDO 或 mysqli

关于php - 如何解决登录页面的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com