保护 Mysqli 语句以避免 SQL 注入,但表单仅发送空白信息

Secured Mysqli Statement to avoid SQL Injection but the form sends only blank Info

美好的一天!我在使用 $stmt 来避免对我的数据库进行 SQL 注入时遇到问题。每当我点击提交时,表单只发送空白的用户名和密码,但已经创建了一个多 ID。 :( 有没有人可以帮助我解决这个问题?

这是我的表格

<form action="index.php" method="POST">
 <h1>Create Username</h1>
    Username: <input type="text" name="user"><br />
    Password: <input type="password" name="pass"><br />
    <input type="submit" value="Submit">
</form>

我的行动清单在这里。

$name = $_POST['user'];
$password = $_POST['pass'];

//$mysqli is my connection stored at my config.php
if ($stmt = $mysqli->prepare("INSERT INTO users (user, password) VALUES (?, ?)")) {

// Bind the variables to the parameter as strings. 
$stmt->bind_param("ss", $name, $password);

// Execute the statement.
$stmt->execute();

// Close the prepared statement.
$stmt->close(); 
}

if($stmt){
 echo "Data Sent";
}else{
 echo "Error in Sending";
}

Mysqli 不像 PDO 那样引用。手动完成。

INSERT INTO `users` (`user`, `password`) VALUES ('?', '?')

为了验证用户,如果您正在使用 php select 的 password_hash() 和 password_verify() 函数,请先检查密码。

如果您使用的是 md5、sha1,...首先 md5 来自 post 的输入密码,然后

 select * from `users` where `username`= '...' AND `password` = 'Here it should be the hashed pw';