2013-07-20 75 views
-4

这是我的代码我不能得到的if语句的工作,如果名称不存在它读取“记录找到”和page3.php说密码不匹配有人可以请帮助我,谢谢我能做些什么来得到这个工作

<?php 
session_start(); 
//$_SESSION["authorized"]=0; 
$name = $_POST["name"]; 
$pass = ($_POST["password"]); 

$connect = mysql_connect("localhost","tina","tinapassword") or die("Could not connect"); 

$selected = mysql_select_db("tinadatabase", $connect) or die("Could not connect to database"); 

$query = "SELECT * FROM users WHERE Uname='$name'"; 
$result = mysql_query($query, $connect); 

$row = mysql_fetch_assoc($result); 

if ($result) 
{ 
    //$row ==1; 
    print "Record found"; 

} 
else 
{ 
    //$row == 0; 
    print "Record not found"; 
} 
print "<br>"; 

md5($pass); 
if($name == $result["Uname"] && md5('$pass') == $result["Upassword"]) 
{ 
    $_SESSION["authorized"] = 1;  
} 
else 
{ 
    $_SESSION["authorized"] = 0; 
} 
print "<br>"; 
print"<a href='page3.php'> continue</a>"; 
?> 
+0

欢迎来到程序员。请阅读[about](http://programmers.stackexchange.com/about)页面。这个问题属于StackOverflow。我会标记它被迁移。 –

+0

抱歉不知道如何使用此页我欣赏帮助 –

+2

您正在使用[an **过时的**数据库API](http://stackoverflow.com/q/12859942/19068)并且应该使用[modern更换](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻击](http://bobby-tables.com/)**,现代的API会使[防御]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己从。 – Quentin

回答

0

正如其他人所说,mysql_num_rows()是你的答案在这里。这里的一个主要问题是,当你应该使用$row时,你也使用$result作为关联数组。我已经重写你的代码:

<?php 

session_start(); 

$name = $_POST["name"]; 
$pass = $_POST["password"]; 

$conn = mysql_connect("localhost","tina","tinapassword") or die("Could not connect to MySQL server."); 

if (!$conn) { 
    echo "Unable to connect to DB: " . mysql_error(); 
    exit; 
} 

if (!mysql_select_db("tinadatabase")) { 
    echo "Unable to select <strong>tinadatabase</strong>: " . mysql_error(); 
    exit; 
} 

$sql = "SELECT * FROM users WHERE Uname='$name'"; 

$result = mysql_query($sql); 

if (!$result) { 
    echo "Could not successfully run query (<strong>$sql<strong>) on DB: " . mysql_error(); 
    exit; 
} 

if (mysql_num_rows($result) == 0) { 
    echo "Record not found."; 
    exit; 
} else 
    echo "Record found." 

$row = mysql_fetch_assoc($result); 

print "<br>"; 

md5($pass); 

if($name == $row["Uname"] && md5($pass) == $row["Upassword"]) { 
    $_SESSION["authorized"] = 1; 
    print "<a href='page3.php'>continue</a>"; 
} else { 
    $_SESSION["authorized"] = 0; 
    print "Username or password incorrect."; 
} 

mysql_free_result($result); 

?> 

现在,我还没有测试过它,但它看起来对我来说很合适。让我知道它是否有效!

+0

谢谢,做了工作我现在阅读正确的数据我真的很感谢帮助 –

0

mysql_query返回查询是否成功。 Findig零记录是成功的。

您必须检查$row,或检查结果集的长度。

注意:检查注释,你的代码有很多问题。

+0

谢谢,但你是什么意思注意:检查评论? –

0

$result将会是一个真正的值 - 即使没有行被返回 - 除非查询出现错误。你需要count the number of rows来查看是否有任何匹配。

(但请参阅我对该问题的评论,首先不应该使用该数据库API)。

0

我认为上面的注释是有用的建议 - 看看使用准备命令和绑定变量。除此之外,您正在引用$ result而不是$ row。

我也会在您检查$ result后执行提取操作。使用(!($ result === false))也是一个好习惯(即测试它不是特别错误的)。我也认为使用!strcmp()来准确地比较字符串是一种很好的做法。说了这么多,你可以通过统计用户名和密码(散列)来简化事情。显然,如果您需要从查询中的用户表中选择其他详细信息,则这不合适。这种方法可以节省你做对了NUM_ROWS等检查强调文本

我不mysql的使用尽可能多的如Oracle和Postgres所以原谅我,如果语法稍有错了,但..假设你UPassowrd使用密码的MD5()哈希...

$hpass=md5($pass); 
$query = "SELECT count(1) FROM users WHERE Uname='$name' and UPassword='$hpass'"; 
$result = mysql_query($query, $connect); 

$_SESSION["authorized"] = 0; 
if (!($result === false)) 
{ 
    $row = mysql_fetch_row($result); 

    echo "Query Worked<br>"; 
    // count(1) result of anything other than 0 is a match - though any more than 1 might be an issue. 
    $_SESSION['authorized']=((intval($row[0]) == 0)?0:1); 
} else { 
    echo "Query Failed<br>"; 
    exit; 
} 
echo "<br>"; 
echo"<a href='page3.php'> continue</a>"; 

不知道你继续HREF到第3页是否是授权值conditonal。

+0

是的,我的page3.php是有条件的授权值,这就是它是什么<?php session_start(); if($ _ SESSION [“authorized”] == 1) { \t print“You logged in”; } else { \t print“密码不匹配,
You must log in first
”; } ?>现在它说密码不匹配,

相关问题