請不要因爲詢問多次詢問過的問題而陷害我。我花了好幾個小時翻閱答案,但找不到解決方案。我只是學習使用PDO。我已經成功地用它從數據庫中選擇並顯示結果,但是我遇到了Update函數的問題。無效的參數編號 - PDO錯誤
我得到錯誤:SQLSTATE [HY093]:無效的參數號:參數未定義。
我已經檢查並重新檢查過我的代碼,但沒有錯別字出現在我身上,所以也許它與edit_date字段有關?如果任何人都可以幫助我,我會非常感激,因爲我花了數小時研究和嘗試沒有解決方案。關心Tog。
這裏是代碼:
$article_id = (trim ($_POST [ 'id' ]) == ' ') ? die ('ERROR:no ID') : mysql_escape_string ($_POST [ 'id' ]);
$article_id = (INT) $article_id;
$title = (trim ($_POST [ 'title' ]) == ' ') ? die ('ERROR: Enter a Title') : mysql_escape_string ($_POST [ 'title' ]);
$author = (trim ($_POST [ 'author' ]) == ' ') ? die ('ERROR: Enter an Author name') : mysql_escape_string ($_POST [ 'author' ]);
$image_url = (trim ($_POST [ 'image_url' ]) == ' ') ? die ('ERROR: Enter an Image URL') : mysql_escape_string ($_POST [ 'image_url' ]);
$main_article = (trim ($_POST [ 'main_article' ]) == ' ') ? die ('ERROR: Enter some article content') : mysql_escape_string ($_POST [ 'main_article' ]);
$snippet = (trim ($_POST [ 'snippet' ]) == ' ') ? die ('ERROR: Enter snippet text') : mysql_escape_string ($_POST [ 'snippet' ]);
$friendly_url = (trim ($_POST [ 'friendly_url' ]) == ' ') ? die ('ERROR: Enter a friendly url') : mysql_escape_string ($_POST [ 'friendly_url' ]);
$meta_title = (trim ($_POST [ 'meta_title' ]) == ' ') ? die ('ERROR: Enter a Meta Title') : mysql_escape_string ($_POST [ 'meta_title' ]);
$meta_description = (trim ($_POST [ 'meta_description' ]) == ' ') ? die ('ERROR: Enter a Meta Description') : mysql_escape_string ($_POST [ 'meta_description' ]);
$edited_by = (trim ($_POST [ 'edited_by' ]) == ' ') ? die ('ERROR: who edited this?') : mysql_escape_string ($_POST [ 'edited_by' ]);
$edit_date = CURRENT_TIMESTAMP;
try {
$dbh = new PDO("mysql:host=$hostname;dbname=gosport", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** Update data ***/
$sql = "UPDATE sport SET title = :title,
author = :author,
image_url = :image_url,
main_article = :main_article,
snippet = :snippet,
edit_date = :edit_date,
friendly_url = :friendly_url,
meta_title = :meta_title,
meta_description = :meta_description,
edited_by = : edited_by
WHERE article_id = :article_id";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':author', $author, PDO::PARAM_STR);
$stmt->bindParam(':image_url', $image_url, PDO::PARAM_STR);
$stmt->bindParam(':main_article', $main_article, PDO::PARAM_STR);
$stmt->bindParam(':snippet', $snippet, PDO::PARAM_STR);
$stmt->bindParam(':edit_date', $edit_date, PDO::PARAM_STR);
$stmt->bindParam(':friendly_url', $friendly_url, PDO::PARAM_STR);
$stmt->bindParam(':meta_title', $meta_title, PDO::PARAM_STR);
$stmt->bindParam(':meta_description', $meta_description, PDO::PARAM_STR);
$stmt->bindParam(':edited_by', $edited_by, PDO::PARAM_STR);
$stmt->execute();
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
感謝您的答覆。我做了一些相應的更改,不再收到錯誤消息,但是當我運行代碼時,它不會更新數據庫。一切和我在運行代碼之前一樣。
我的新代碼:
$article_id = (trim ($_POST [ 'id' ]) == ' ');
$article_id = (INT) $article_id;
$title = (trim ($_POST [ 'title' ]) == ' ');
$author = (trim ($_POST [ 'author' ]) == ' ');
$image_url = (trim ($_POST [ 'image_url' ]) == ' ');
$main_article = (trim ($_POST [ 'main_article' ]) == ' ');
$snippet = (trim ($_POST [ 'snippet' ]) == ' ');
$friendly_url = (trim ($_POST [ 'friendly_url' ]) == ' ');
$meta_title = (trim ($_POST [ 'meta_title' ]) == ' ');
$meta_description = (trim ($_POST [ 'meta_description' ]) == ' ');
$edited_by = (trim ($_POST [ 'edited_by' ]) == ' ');
$edit_date = CURRENT_TIMESTAMP;
try {
$dbh = new PDO("mysql:host=$hostname;dbname=gosport", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** Update data ***/
$sql = "UPDATE sport SET title = :title,
author = :author,
image_url = :image_url,
main_article = :main_article,
snippet = :snippet,
edit_date = :edit_date,
friendly_url = :friendly_url,
meta_title = :meta_title,
meta_description = :meta_description,
edited_by = :edited_by
WHERE article_id = :article_id";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':article_id', $article_id, PDO::PARAM_STR);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':author', $author, PDO::PARAM_STR);
$stmt->bindParam(':image_url', $image_url, PDO::PARAM_STR);
$stmt->bindParam(':main_article', $main_article, PDO::PARAM_STR);
$stmt->bindParam(':snippet', $snippet, PDO::PARAM_STR);
$stmt->bindParam(':edit_date', $edit_date, PDO::PARAM_STR);
$stmt->bindParam(':friendly_url', $friendly_url, PDO::PARAM_STR);
$stmt->bindParam(':meta_title', $meta_title, PDO::PARAM_STR);
$stmt->bindParam('meta_description', $meta_description, PDO::PARAM_STR);
$stmt->bindParam(':edited_by', $edited_by, PDO::PARAM_STR);
$stmt->execute();
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
當然,我可以在你的SQL中看到11個佔位符,只有10個參數被綁定。你在哪裏綁定':article_id'? – George 2014-10-20 14:27:47
所有對'mysql_real_escape_string()'的調用都是不必要的,在這種情況下是無效的。不需要轉義,因爲PDO將在綁定參數時處理該轉義。 'mysql_real_escape_string()'只能通過'mysql_query()'激活連接。 – 2014-10-20 14:28:35
混合MySQL API。與*杜松子酒不同,這些'mysql_'和PDO不會。 – 2014-10-20 14:28:57