2017-05-06 30 views
0

我得到這個錯誤, PDOStatement :: execute():SQLSTATE [HY093]:無效的參數編號:綁定變量的數量與/ storage/h14/202/1552202/public_html中的標記數量不匹配/第38行的signup.php。 這是怎麼回事?我看不到它。Mysql錯誤093

<?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()); 
    } 
} 
?> 
+0

SHA1不安全 –

+0

SQL文本中不包含':password'佔位符。回聲SQL文本,你會看到發生了什麼......'sha1'函數調用正在使用字符串參數..並且函數的返回值正被包含在SQL文本中。 https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – spencer7593

回答

0

你不能像這樣使用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()); 
    } 
} 
?>