2014-10-17 91 views
0

我是MySQL的新手。我创建了一个运行PHP来查询数据库的登录页面。mysql准备好的语句不起作用

我想做一个准备好的语句来防止SQL注入,但它不工作。 $结果给了我0.1

的cgi-bin /的welcome.php

<?php 
$email= $_POST["email"]; 
$pwd = $_POST["password"]; 

$dbh=mysqli_connect("localhost", 
"zzengnin",<password>,"zzengnin_wheel") 
or die ('Database is not able to connect'); 

$qry= "SELECT email FROM Info WHERE email=? AND pw=?"; 

if ($stmt = mysqli_prepare($link, $qry)) { 
    mysqli_stmt_bind_param($stmt, "ss", $email, $pwd); 
    mysqli_stmt_execute($stmt); 

    mysqli_stmt_bind_result($stmt, $result); 
    mysqli_stmt_fetch($stmt); 
    printf("result email is %s\n", $result); 
} 


if(mysqli_num_rows($result)>0) 
{ 
    echo "Welcome! '$email', you are officially logged in! "; 
} 
else{ 
    echo "Your Email or Password is WRONG!!!!"; 

    echo $result + " result"; 
} 
mysqli_stmt_close($stmt); 
mysqli_close($dbh); 
?> 

这是形式的login.php

<!DOCTYPE html> 
<head><title>Wheel Sharing</title> 
<link rel = "stylesheet" type = "text/css" href = "CSS/style.css"> 
</head> 
<body> 
<h1>Log in</h1> 

<form action="cgi-bin/welcome.php" method="post"> 
Email: <input name="email" type="text" size="20"/> 
<br/> 
Password: <input name="password" type="password" > 
<br/> 
<input type="submit" value="Login"/> <input type="reset" value="Reset"/> 

</form> 

</body> 
</html> 

所有帮助表示赞赏:)

+1

如果您正在移植到'mysqli',这是一件好事,面向对象的界面通常不那么杂乱。那就是你使用'$ stmt-> bind_param()'而不是老派的程序等价物,这是PHP4的遗物。 – tadman 2014-10-17 21:19:13

+1

这也是非常令人担忧的是,您似乎没有采取任何预防措施[正确哈希用户密码](http://www.phptherightway.com/#password_hashing),暴露您的用户相当大的风险,如果您的数据库被泄密。 – tadman 2014-10-17 21:20:37

+0

您需要捕获错误并将其返回,以便知道发生了什么。你的错误日志发生了什么? – 2014-10-17 21:21:32

回答

1

更改此:

mysqli_stmt_execute($stmt); 

对此:

$result = mysqli_stmt_execute($stmt);