2017-04-25 21 views
-1

CMSで異なるフィールド(名前など)を編集できるようにしようとしています。私は「更新」を押すと、しかし、私は次のエラーを取得する:データベースを更新する際に「無効なパラメータ番号」が表示される理由は何ですか?

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in /studenthome.hallam.shu.ac.uk/STUDENTHOME10/1/b5035381/public_html/affinity/cms/process/editRecord.php: in /studenthome.hallam.shu.ac.uk/STUDENTHOME10/1/b5035381/public_html/affinity/cms/process/editRecord.php on line 28 PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /studenthome.hallam.shu.ac.uk/STUDENTHOME10/1/b5035381/public_html/affinity/cms/process/editRecord.php on line 28 Call Stack: 0.0029 659144 1. {main}() /studenthome.hallam.shu.ac.uk/STUDENTHOME10/1/b5035381/public_html/affinity/cms/process/editRecord.php:0 0.0135 672928 2. PDOStatement->execute() /studenthome.hallam.shu.ac.uk/STUDENTHOME10/1/b5035381/public_html/affinity/cms/process/editRecord.php:28

ここに私のコードです:

<?php 
ini_set('display_errors', 1); 

// add your includes for connections and functions 
// make sure the path is correct 
require ('../../includes/conn.inc.php'); 
require ('../../includes/functions.inc.php'); 

// sanitize user variables 
$splayerName = safeString($_POST['playerName']); 
$splayerDescription = safeString($_POST['playerDescription']); 
$splayerImage = safeString($_POST['playerImage']); 
$splayerRank = safeString($_POST['playerRank']); 
$splayerSpec = safeString($_POST['playerSpec']); 
$splayerID = safeInt($_POST['playerID']); 

// build prepare statement 
$sql = "UPDATE affinity SET playerName = :playerName, 
playerDescription = :playerDescription, 
playerImage = :playerImage, 
playerRank = :playerRank, 
playerSpec = :playerSpec 
WHERE playerID = :playerID"; 
$stmt = $pdo->prepare($sql); 

$stmt->bindParam(':playerName', $splayerName, PDO::PARAM_STR); 
$stmt->bindParam(':playerDescription', $splayerDescription, PDO::PARAM_STR); 
$stmt->bindParam(':playerImage', $splayerImage, PDO::PARAM_STR); 
$stmt->bindParam(':playerRank', $splayerRank, PDO::PARAM_STR); 
$stmt->bindParam(':playerRank', $splayerRank, PDO::PARAM_STR); 
$stmt->bindParam(':playerSpec', $splayerSpec, PDO::PARAM_INT); 
$stmt->execute(); 

// redirect browser 
header("Location: ../cms.php"); 

// make sure no other code executed 
exit; 
?> 

これが動作しない理由を私はよく分かりません。どうすれば修正できますか?

+1

5つの変数をバインドしていますが、6つのフィールドを使用しています。ランクが2倍になっている – RST

+0

コード内のエラーをチェックしないため、何が問題なのか分かりません。コードが常に完璧に動作するとは決して考えないでください。 –

答えて

0

あなたのplayerIDのバインドはどこですか?それが原因です。 You'eバインディングランクは2回、IDは決してありません。

$stmt->bindParam(':playerName', $splayerName, PDO::PARAM_STR); 
$stmt->bindParam(':playerDescription', $splayerDescription, PDO::PARAM_STR); 
$stmt->bindParam(':playerImage', $splayerImage, PDO::PARAM_STR); 
$stmt->bindParam(':playerRank', $splayerRank, PDO::PARAM_STR); 
$stmt->bindParam(':playerSpec', $splayerSpec, PDO::PARAM_INT); 
$stmt->bindParam(':playerID', $splayerID, PDO::PARAM_STR); 
+0

あなたの権利はそれを指摘するためにありがとう – unreal1357

0
<?php 
ini_set('display_errors', 1); 

// add your includes for connections and functions 
// make sure the path is correct 

require ('../../includes/conn.inc.php'); 

require ('../../includes/functions.inc.php'); 

// sanitize user variables 

$splayerName = safeString($_POST['playerName']); 
$splayerDescription = safeString($_POST['playerDescription']); 
$splayerImage = safeString($_POST['playerImage']); 
$splayerRank = safeString($_POST['playerRank']); 
$splayerSpec = safeString($_POST['playerSpec']); 
$splayerID = safeInt($_POST['playerID']); 

// build prepare statement 

$sql = "UPDATE affinity SET playerName = :playerName, 
playerDescription = :playerDescription, 
playerImage = :playerImage, 
playerRank = :playerRank, 
playerSpec = :playerSpec 
WHERE playerID = :playerID"; 
$stmt = $pdo->prepare($sql); 
$stmt->bindParam(':playerName', $splayerName, PDO::PARAM_STR); 
$stmt->bindParam(':playerDescription', $splayerDescription, PDO::PARAM_STR); 
$stmt->bindParam(':playerImage', $splayerImage, PDO::PARAM_STR); 
$stmt->bindParam(':playerRank', $splayerRank, PDO::PARAM_STR); 
$stmt->bindParam(':playerSpec', $splayerSpec, PDO::PARAM_STR); 
$stmt->bindParam(':playerID', $splayerID, PDO::PARAM_INT); 
$stmt->execute(); 

// redirect browser 

header("Location: ../cms.php"); 

// make sure no other code executed 

exit; 
?> 
1

:playerRank

2回バインドされていて、

:playerID

が拘束されていません。

そして:

:playerSpec

は、文字列ではなく、int型としてバインドされなければなりません。

$stmt->bindParam(':playerName', $splayerName, PDO::PARAM_STR); 
$stmt->bindParam(':playerDescription', $splayerDescription, PDO::PARAM_STR); 
$stmt->bindParam(':playerImage', $splayerImage, PDO::PARAM_STR); 
$stmt->bindParam(':playerRank', $splayerRank, PDO::PARAM_STR); 
$stmt->bindParam(':playerSpec', $splayerSpec, PDO::PARAM_STR); 
$stmt->bindParam(':playerID', $splayerID, PDO::PARAM_INT); 
関連する問題