2017-03-16 80 views
1

你好,這裏是我的代碼,我創建了2 SQLite表樣品和login_info正確,雖然這個錯誤仍然彈出問題與PDO結合/執行

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 25 bind or column index out of range in C:\xampp\htdocs\display.php:22

<?php 
session_start(); 
$user = $_POST['username'] ?? ''; 
$pword = $_POST['password'] ?? ''; 
$_SESSION['details'] = [ 
    'user' => $user, 
    'pword'=> $pword 
]; 
$pdo = new PDO('sqlite:'.__DIR__.'/users.db'); 
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 

if (!isset($_SESSION['sample'])){ 
    $statement = $pdo->prepare("INSERT INTO sample(timecreated) VALUES (?)"); 
    $statement->execute([time()]); 
    $_SESSION['sample'] = $pdo->lastInsertId(); 
} 
$deleteStatement = $pdo->prepare("DELETE FROM login_info WHERE employee_id=?"); 
$deleteStatement->execute([$_SESSION['sample']]); 

這是我的錯誤開始

$insertStatement = $pdo->prepare("INSERT INTO login_info (username,password) VALUES (?,?)"); 
$insertStatement->execute([$_SESSION['sample'],'username', $user]); 
$insertStatement->execute([$_SESSION['sample'],'password', $pword]); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Display</title> 
</head> 
<body> 
    <p> 
     Your username is <?php echo $user ?> and your password <?php echo $pword ?>. 
    </p> 
    <a href="login.php">Go Back</a> 
    </body> 
</html>' 
+0

是否'$ user'和'$ pword'攜帶價值? –

回答

1

你的問題是你沒有綁定這兩個值,你試圖單獨執行它們。先綁定它們,然後執行,或者同時執行它們。您還提供3個參數,而不是兩個。我看不到$_SESSION['sample'](來自先前查詢的ID)如何適應該問題,因此只需將其刪除即可。

下面是一個如何執行它的例子。請注意,在​​中作爲參數提供的數組與佔位符的元素完全相同,並且它們以正確的順序出現。

$insertStatement = $pdo->prepare("INSERT INTO login_info (username, password) VALUES (?, ?)"); 
$insertStatement->execute([$user, $pword]); 

你的問題是你試圖執行多次。你似乎與PDOStatement::bindParam語法,這將是

$insertStatement = $pdo->prepare("INSERT INTO login_info (username, password) VALUES (?, ?)"); 
$insertStatement->bindParam(1, $user); 
$insertStatement->bindParam(2, $pword); 
$insertStatement->execute(); 

通過調用​​多時間對同一語句混淆,您嘗試多次執行它 - 這是有用的,如果要插入相同類型的數據,但具有不同的值 - 這與您在此處所做的事無關。


其他注意事項...

您當前密碼存儲在一個會話,並將它們直接顯示給用戶 - 這是強烈建議反對。它也看起來像你以純文本存儲psaswords,這是一個大號不 -!使用適當的哈希方法,如password_hash()password_verify()

參考& Readingmaterial