我有問題,不能正常工作好,不要編輯數據PHP MySQL的PDO功能無法使用此代碼編輯數據
edit.php
<?php
$data = array(':news_id' => $_POST['news_id'] , ':news_title' => $_POST['news_title'] , ':news_content' => $_POST['news_content']);
$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "acp";
try {
$dbh = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*if (isset($_POST['news_id'])) {
$query = "UPDATE news SET 'news_title'=:news_title, 'news_content'=:news_content WHERE news_id=:news_id";
$sth = $dbh -> prepare($query);
$sth -> execute($data);
} */
$id = isset($_GET['news_id']) ? $_GET['news_id'] : NULL;
$sth = $dbh -> prepare("UPDATE news SET `news_title`=:news_title, `news_content`=:news_content WHERE `news_id`=:news_id");
$sth -> bindValue(":news_id", $_GET["news_id"], PDO::PARAM_INT);
$sth -> bindValue(":news_title", $_GET["news_title"]);
$sth -> bindValue(":news_content", $_GET["news_content"]);
$sth -> execute();
$sth -> closeCursor();
header('Location: ../news_admin.php');
}
catch(PDOException $e) {
echo $e->getMessage();
}
$dbh = null;
?>
您是否收到任何錯誤? – Michael
**不起作用**不能幫助我們理解問題所在。定義「* work *」意思是通過解釋你想要的代碼做什麼,它沒有做什麼或代碼做了什麼,你沒有期望它做。 – Sherif
您確定在GET中有這些綁定值參數,即在url中?你可能需要$ _POST – Mihai