mysql 登录框注入,在PHP的登录页面中防止SQL注入

I have a login page (login.php) and below is the code I am trying to use to authenticate the user by using mysqli_prepare:

if(!$_POST["username"] || !$_POST["password"])

{

echo "Username and Password fields are mandatory.";

}

else

{

$unsafe_username = $_POST["username"];

$unsafe_password = md5($_POST["password"]);

$query=mysqli_prepare($con, "select user_id from users where user_name= ? and password= ? ");

$query->bind_param("ss", $unsafe_username, $unsafe_password);

$query->execute();

$data = $query->get_result();

if(is_array($data))

{

echo "Login successful";

}

else

{

echo "Login failed";

}

}

Below is the code used for making MYSQL connection:

function connect_database()

{

$con = mysqli_connect("servername", "username", "", "dbname");

if (!$con)

{

$con = "";

echo("Database connection failed: " . mysqli_connect_error());

}

return $con;

}

By using mysqli_prepare, I am getting following warning and error messages:

Warning: mysqli_prepare() expects exactly 2 parameters, 1 given

Fatal error: Call to a member function bindParam() on null

I asked the similar question two days ago, but did not get any complete solution. (Prevent SQL Injection to Login Page in PHP and MySQL).

Please help me in resolving the problem. Is it the best approach to prevent my DB from SQL Injection?

解决方案

missing connection variable $con

$query=mysqli_prepare($con,"select user_id from users where user_name= ? and

password= ? ");

and change this too

$query->bind_Param("ss", $unsafe_username, $unsafe_password);

^^^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值