我收到此错误, PDOStatement::execute(): SQLSTATE[HY093]: 参数数量无效:绑定(bind)变量的数量与第 38 行/storage/h14/202/1552202/public_html/signup.php 中的标记数量不匹配。 这有什么问题吗?我看不到。
<?php
if (isset($_POST['signup'])){
$username = $_POST['Username'];
$password = $_POST['Password'];
try {
$db = new PDO('mysql:host=localhost;dbname=Accounts', 'id1552202_thecouch', 'Fargo123');
$query = $db->prepare(" INSERT INTO Accounts(Usernames, Passwords) VALUES (:username ,". sha1(":password") .")");
$query->bindParam("username", $username);
$query->bindParam("password", $password);
if($query->execute()){
echo "<center>Account made</center>";
} else {
echo "Error";
}
} catch(PDOException $e){
die("Error!: " . $e->getMessage());
}
}
?>
最佳答案
你不能那样使用 sha1()。
首先,不要使用 sha1。使用 sha2/sha256。实际上你也应该使用 hmac。 PHP 有一个非常好的密码散列工具。了解更多!但这不是你的问题...
其次,在构建查询时,您正在计算字符串文字“:password”的 sha1(),因此它不再是查询中的占位符。您需要在查询字符串之外获取密码的 sha1() 并将结果作为参数传递。
像这样:
<?php
if (isset($_POST['signup'])){
$username = $_POST['Username'];
$password = sha1($_POST['Password']);
try {
$db = new PDO('mysql:host=localhost;dbname=Accounts', 'id1552202_thecouch', 'Fargo123');
$query = $db->prepare(" INSERT INTO Accounts(Usernames, Passwords) VALUES (:username , :password )");
$query->bindParam("username", $username);
$query->bindParam("password", $password);
if($query->execute()){
echo "<center>Account made</center>";
} else {
echo "Er is een fout opgetreden";
}
} catch(PDOException $e){
die("Error!: " . $e->getMessage());
}
}
?>
关于php - Mysql错误093,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43815944/