【发布时间】:2015-10-18 12:52:53
【问题描述】:
大家好,我正在尝试为登录编写一些代码,我已经掌握了所有内容,但每当我尝试登录时,它都会显示无法选择数据库。我想不通,我用 One.com 托管,当我进入 PHP 和 MySQL 设置时,它说数据库名称是“c343_co_uk”,这是我在下面的代码中使用的:
更新:它正在检测数据库,但每当我尝试使用与 MySQL 上完全相同的用户名和密码登录时,它都会显示无效登录
这是我的Connection.php
<?php
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
//connection to the database
$connection = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("c343_co_uk",$dbhandle)
or die("Could not select Database");
?>
Loginform.php
<!DOCTYPE html>
<html>
<a href="index.html" title="back to home">Home</a>
</font>
<head>
<style>
body {
font-size: 14px;
}
</style>
<link rel="stylesheet" type="text/css" href="website.css" />
</head>
<body>
<div id="loginform" style="font-family: 'ClearSans-Thin'; color: Black">
Please enter your login details<br /><br />
Username:<br />
<form method="post" action="loginsubmit.php">
<input type="text" name="username" />
<br />
Password:<br />
<input type="password" name="password" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</center>
</div>
</body>
</html>
登录提交.php
<?php
session_start();
?>
<font face="ClearSans-Thin">
<font color="lightgray">
<?php
include 'connection.php';
include 'loginform.php';
?>
<center>
<?php
if (isset($_POST['submit']))
{
$user = $_POST['username'];
$pass = $_POST['password'];
//Counts up how many matches there are in the database
$query = "SELECT COUNT(*) AS cnt FROM users WHERE Username='" . mysqli_real_escape_string($connection, $user) . "' && Password='" . mysqli_real_escape_string($connection, $pass). "'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$queryadmin = "SELECT COUNT(*) AS cnt FROM admin WHERE Username='" . mysqli_real_escape_string($connection, $user) . "' && Password='" . mysqli_real_escape_string($connection, $pass). "'";
$resultadmin = mysqli_query($connection, $queryadmin);
$rowadmin = mysqli_fetch_assoc($resultadmin);
//If count is more than 0, log user in.
if ($row["cnt"] > 0)
{
$_SESSION["userlogged"] = $user;
echo "Logged in - Press the home button to return to the homepage";
}
//count for user table is 0, if there are more than 0 matches in the admin database, start admin session
else if ($rowadmin["cnt"] > 0 )
{
$_SESSION["adminlogged"] = $user;
echo "Logged in - Press the home button to return to the homepage";
}
else
{
echo 'Not a valid login';
}
}
?>
</center>
【问题讨论】:
-
你在哪里定义
$dbhandle? -
试试
echo mysql_error()看看实际的错误信息是什么。 -
我用连接交换了 dbhandle,现在它正在连接,我的错!但是它不能识别表中的凭据,所以说无效登录。
标签: php html mysql connection