-1

I'm using mysqli to resist second-order injection attacks. My code (your can't pay attention to the spelling of letters, just pay attention to the questions I raised) is:

$sql = "select * from users where username = ? and passcode= ? ";
$stmt = $con->prepare($sql);
if (!$stmt) exit("prepare执行错误"");
$stmt->bind_param("ss", $username,Spasswd);
$stmt->execute();
if(Sstmt->affected_rows>0)
{
$result = $stmt->get_result();
$Srow = $result->fetch_row();
if ($row)
{
session_start();
$_SESSION['username']= $row[1];
echo $row[1].'欢迎访问!";
echo "<br>";
echo "<a href='updatepasswd.html'>修改密码</a>";
}
else
{
echo "<script=alert('用户名或密码错误!"); history.go(-1);</script>";
}
$stmt--close();
}
else{
die('sQL语句执行失败, : '.mysqli_error($con));}

Some users in my database have:"admin"和"admin'#" However, no matter which login I use, the SQL statement fails.If you change "if ($stmt - > affected_rows > 0)" to "if ($stmt - > execute())", no matter which one you use, you will be prompted with the wrong user name or password (but I typed it correctly).The search data says that "execute()" only judges the correctness of the SQL statement and does not care about the results, but my SQL is a query statement. I feel that there is no change in the line after execution, so I don't know what to do. My problem now is to change this query into a parametric query and log in to the account with special characters.

Dharman
  • 26,923
  • 21
  • 73
  • 125
han jiang
  • 9
  • 1
  • https://www.php.net/manual/en/mysqli.affected-rows.php: _"Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query. "_ - you have neither of these statement types here. – CBroe May 23 '22 at 13:43
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman May 23 '22 at 14:05
  • holy jesus, so many errors in this code... start fixing those... – Honk der Hase May 23 '22 at 16:14

0 Answers0