This question already has an answer here:
这个问题在这里已有答案:
- Can I mix MySQL APIs in PHP? 5 answers
- 我可以在PHP中混合MySQL API吗? 5个答案
I created an account on 000webhost to create html page that has a login form. But I am facing errors when i try to connect the form to the database.
我在000webhost上创建了一个帐户来创建具有登录表单的html页面。但是当我尝试将表单连接到数据库时,我遇到了错误。
Here is the html code:
这是html代码:
<html>
<head>
<title>Login Page</title>
<style type = "text/css">
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
label {
font-weight:bold;
width:100px;
font-size:14px;
}
.box {
border:#666666 solid 1px;
}
</style>
</head>
<body bgcolor = "#FFFFFF">
<div align = "center">
<div style = "width:300px; border: solid 1px #333333; " align = "left">
<div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
<div style = "margin:30px">
<form method="POST" action="C1.php">
<label>UserName :</label><input type = "text" name = "username" required = "box"/><br /><br />
<label>Password :</label><input type = "password" name = "password" required = "box" /><br/><br />
<input type = "submit" name="submit" value = " Login "/><br />
</form>
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
</div>
</div>
</div>
</body>
</html>
here is my php page
这是我的php页面
<?php
session_start();
$servername = "localhost";
$username = "******";
$password = "******";
// Create connection
$database_name = "id2425621_login";
$conn = mysqli_connect($servername, $username, $password, $database_name);
if (isset($_POST['submit']))
{
$username='king';
$password='king123';
$query = mysqli_query($conn, "SELECT * FROM mylogin WHERE username='$username' and password='$password'");
if (mysqli_num_rows($query) != 0)
{
echo "sucess";
}
else
{
echo "fail";
}
}
?>
mysql has a table named mylogin and has the values:
mysql有一个名为mylogin的表,其值为:
INSERT INTO `mylogin`(`username`, `password`) VALUES ('king','king123')
i couldn't connect to the database, appreciate ur help in advance
我无法连接到数据库,提前感谢您的帮助
- code updated
- 代码已更新
2 个解决方案
#1
1
You need to change this line:
您需要更改此行:
<input type = "submit" value = " Login "/>
To this:
对此:
<input type = "submit" name="submit" value = " Login "/>
The if()
statement in your php wasn't picking up on $_POST['submit']
because it didn't exist.
你的php中的if()语句没有在$ _POST ['submit']上获取,因为它不存在。
You're also mixing mysql_
and mysqli_
Everything needs to be mysqli_
given that mysql_
is deprecated and generally considered unsafe.
你也混合了mysql_和mysqli_所有东西都需要是mysqli_,因为mysql_已被弃用,通常被认为是不安全的。
#2
0
You did not mention about databasae. Your MySqli Connection needs the database parameter.
你没有提到databasae。您的MySqli连接需要数据库参数。
$conn = mysqli_connect($servername, $username, $password);
should be
应该
$database_name = "foo";
$conn = mysqli_connect($servername, $username, $password, $database_name);
First you can check whether your connection is correct using below code.
首先,您可以使用以下代码检查您的连接是否正确。
if($conn){
echo "Success";
}
else{
echo "Error Connection";}
AND
和
MySQL Functions should be according to MySQLi
MySQL函数应该根据MySQLi
$query = mysqli_query($conn, "SELECT * FROM mylogin WHERE username='$username' and password='$password'");
if (mysqli_num_rows($query) != 0)
#1
1
You need to change this line:
您需要更改此行:
<input type = "submit" value = " Login "/>
To this:
对此:
<input type = "submit" name="submit" value = " Login "/>
The if()
statement in your php wasn't picking up on $_POST['submit']
because it didn't exist.
你的php中的if()语句没有在$ _POST ['submit']上获取,因为它不存在。
You're also mixing mysql_
and mysqli_
Everything needs to be mysqli_
given that mysql_
is deprecated and generally considered unsafe.
你也混合了mysql_和mysqli_所有东西都需要是mysqli_,因为mysql_已被弃用,通常被认为是不安全的。
#2
0
You did not mention about databasae. Your MySqli Connection needs the database parameter.
你没有提到databasae。您的MySqli连接需要数据库参数。
$conn = mysqli_connect($servername, $username, $password);
should be
应该
$database_name = "foo";
$conn = mysqli_connect($servername, $username, $password, $database_name);
First you can check whether your connection is correct using below code.
首先,您可以使用以下代码检查您的连接是否正确。
if($conn){
echo "Success";
}
else{
echo "Error Connection";}
AND
和
MySQL Functions should be according to MySQLi
MySQL函数应该根据MySQLi
$query = mysqli_query($conn, "SELECT * FROM mylogin WHERE username='$username' and password='$password'");
if (mysqli_num_rows($query) != 0)