2014-12-04 37 views
-2

我已經看到了一些類似的帖子,但我不能與之相關的,所以這裏有雲:參數錯誤 - PHP,MySQLi的登錄表單

建築形式登錄,用PHP。我不熟悉MySQli,我知道我的一些代碼有點混合。無論如何,我不斷收到:

mysqli_query()預計參數1是mysqli的,串給出線15個
mysqli_num_rows()期望的是1個參數,2給出線16

你能大綱,顯然,我要去哪裏錯了!除此之外,我知道我需要添加一些更好的安全性,但它只是想讓它工作,因爲我已經嘗試了這麼久了。

任何其他的想法是大量讚賞!

<?php 
include "connect.php"; 
// Checking to see if the login form has been submitted 

if (isset($_POST['username'])) { 
$username = $_POST['username']; 
$password = $_POST['password']; 
$sql = "SELECT * FROM users WHERE username = $username AND password = $password LIMIT 1"; 

$result=mysqli_query($sql); 
$num_rows = mysqli_num_rows($result); 
$row=mysqli_fetch_assoc($result); 

// If login information is correct 
if ($num_rows == 1) { 
    echo "You have successfully logged in."; 
    exit(); 
} 
// If login information is invalid 
else { 
    echo "Invalid login information. Please return to the previous page."; 
    exit(); 
} 
} 

?> 


<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<h1>Login</h1> 
<form method="post" action="login.php"> 
Username: <input type="text" name="username" /><br /><br /> 
Password: <input type="password" name="password" /><br /><br /> 
<input type="submit" name="submit" value="Log In" /> 
</form> 
</body> 
</html> 

感謝您的關注。

+0

檢查文檔的'mysqli_query':http://php.net/manual/en/mysqli.query.php(提示:它比'的mysql_query不同') – 2014-12-04 21:16:42

+0

您必須將鏈接傳遞到您的連接:)它存儲在connection.php – Toumash 2014-12-04 21:17:54

+0

您需要設置編碼aswell並且轉義POST變量+把引號放在像''SELECT * FROM users WHERE username ='「 。 mysqli_real_escape_string($ username)。 「'AND ...' – DanFromGermany 2014-12-04 21:18:03

回答

0

只要您在使用mysqli時進行查詢,就必須傳遞連接。看看mysqli_connect()

0

mysqli_query()需要兩個參數:一個連接和一個查詢。你缺少的連接:

$result=mysqli_query($conn, $sql); 

我不知道在哪裏/它是如何在你的connect.php定義的,但你需要補充一點,纔可以查詢任何東西。

0

mysqli_query預計第一個參數是連接資源

例如,

$connection=mysqli_connect("localhost","root","") or die("Failed to connect with MySQL."); 
mysqli_query($connection,"SQL HERE"); 

在你的情況,

$result = mysqli_query($YOUR_CONNECTION_VARIABLE,$sql);