簡體   English   中英

使用會話變量訪問sql數據庫table->列條目

[英]Using session variable to access sql database table-> column entries

這是我遇到的問題。

我創建了一個登錄頁面,用戶輸入他的名字並傳遞進入。 數據庫已經創建,我將輸入的登錄信息存儲在會話變量中。

在單擊提交時,我將用戶重定向到php訪問mysql數據庫的頁面,並在數據庫中搜索用戶名並使用會話變量傳遞組合。

而且存在問題。 會話變量無法訪問數據庫表條目。

這是我的代碼:

<?php
//starting the session
session_start();

//connecting to database and table "testdb"
@mysql_connect("localhost","root","") or die("no connect");
@mysql_select_db("testdb") or die ("no select");


//the session variables holding the username and password
//trying to access the entries in table named "table"
$sql="SELECT * FROM `table` WHERE name='.$_SESSION['uname'].' AND
pass='.$_SESSION['pass'].'";

$query=mysql_query($sql);

//printing the result which is just one entry
while($result=mysql_fetch_array($query))

{

echo $result['name'].' ';
echo $result['pass'];

}

?>

我不知道錯誤是什么,或者我是否使用了錯誤的語法。 我沒有寫實際代碼的評論......這只是一個相同的例子:D

我不是在尋找替補,因為我現在處於學習階段。 因此,我們將非常感謝對此代碼的任何修復。

PS:我不懂java腳本。

您的字符串語法不正確,這實際上會生成一個解析錯誤

$sql = "SELECT * FROM `table` WHERE name='" .$_SESSION['uname']. "' AND
pass = '" .$_SESSION['pass'] ."'";

在使用$_SESSION['key']之前,您需要實際關閉字符串,或者不使用引號以便對其進行插值。

此代碼也極易受到注入攻擊。 您應該使用PDO參數化查詢。

$stmt = $pdo->prepare("SELECT * FROM `table` WHERE name = ? AND pass = ?");
$stmt->execute(array($_SESSION['uname'], $_SESSION['pass']));

您的查詢有語法錯誤。 您需要在會話值周圍加上雙引號或使用大括號代替。 試試 -

$sql="SELECT * FROM `table` WHERE name='".$_SESSION['uname']."' AND
pass='".$_SESSION['pass']."'";

要么

$sql="SELECT * FROM `table` WHERE name='{$_SESSION['uname']}' AND
pass='{$_SESSION['pass']}'";

最明顯的是,這完全容易受到SQL注入攻擊。 但是忽略了......

$sql="SELECT * FROM `table` WHERE name='.$_SESSION['uname'].' AND pass='.$_SESSION['pass'].'";

... 應該 ...

$sql="SELECT * FROM `table` WHERE name='".$_SESSION['uname']."' AND pass='".$_SESSION['pass']."'";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM