我有一個帶有「Telefoonnummers」表的數據庫「Telefoon」。該表有「naam」,「數字」和「mobiel」列用PHP/PDO和mySQL更新數據庫記錄
我想在一個.php頁面更新記錄。我很確定這是對的,但不知何故沒有改變。
在這裏你填寫更新的記錄,當你點擊按鈕時,它會進入更新它的下一頁。
<!DOCTYPE HMTL>
<html>
<head>
<title>Wijzigen telefoonnummer</title>
</head>
<body>
<?php
$record_name = $_GET["naam"];
$user = "root";
$pass = "root";
$dbh = new PDO(
'mysql:host=localhost; port=8473; dbname=telefoon',
$user,
$pass
);
$sth = $dbh -> prepare("
SELECT *
FROM Telefoonnummers
WHERE naam = :record_name
");
$sth -> bindValue(":record_name", $record_name, PDO::PARAM_STR);
$sth -> execute();
$printRecord = $sth -> fetchAll(PDO::FETCH_ASSOC);
/*
//dit record als array weergeven
print("<pre>");
print_r($printRecord);
print("</pre>");
*/
//gegevens in variabelen zetten
$printRecordRecord = $printRecord[0];
$huidigeNaam = $printRecordRecord["naam"];
$huidigeNummer = $printRecordRecord["telefoonnummer"];
$huidigeMobiel = $printRecordRecord["mobiel"];
//niet meer nodig door bovenstaande
/*
foreach($printRecord AS $printRecordIndex => $printRecordRecord) {
$huidigeNaam = $printRecordRecord["naam"];
$huidigeNummer = $printRecordRecord["telefoonnummer"];
$huidigeMobiel = $printRecordRecord["mobiel"];
}
*/
print("
<form action='wijzig.php' method='POST'>
<table>
<tr>
<td bgcolor='green'><b>Naam</b></td>
<td bgcolor='green'><b>Telefoonnummer</b></td>
<td bgcolor='green'><b>Mobiel</b></td>
<td bgcolor='green'></td>
</tr>
<tr>
<td><input type='text' name='naam' value='$huidigeNaam' disabled='TRUE' /></td>
<td><input type='text' name='nummer' value='$huidigeNummer' /></td>
<td><input type='text' name='mobiel' value='$huidigeMobiel' /></td>
<td><input type='submit' value='Wijzig' /></td>
</tr>
</table>
</form>
");
?>
</body>
這實際上改變(至少這是我想要的)記錄下一個頁面:
<!DOCTYPE HTML>
<html>
<head>
<title>Gewijzigd</title>
</head>
<body>
<?php
//geupdate gegevens ophalen
$newNaam = $_POST["naam"];
$newNumber = $_POST["nummer"];
$newMobile = $_POST["mobiel"];
//gegevens updaten als ALLES is ingevuld
if (($newNaam != "") && ($newNumber != "") && ($newMobile != "")) {
$user = "root";
$pass = "root";
$dhb = new PDO(
'mysql:host=localhost; port=8473; dbname=telefoon',
$user,
$pass
);
$sth = $dbh -> prepare("
UPDATE Telefoonnummers
SET naam = :naam,
telefoonnummer = :nummer,
mobiel = :mobiel
WHERE naam = :naam
");
$sth -> bindValue(":naam", $newNaam, PDO::PARAM_STR);
$sth -> bindValue(":telefoonnummer", $newNumber, PDO::PARAM_STR);
$sth -> bindValue(":mobiel", $newMobile, PDO::PARAM_STR);
$sth -> execute();
$sthCheck = $dbh -> prepare("
SELECT *
FROM Telefoonnummers
WHERE naam = :naam
");
$sthCheck -> bindValue(":naam", $newNaam, PDO::PARAM_STR);
$sthCheck -> execute();
}
?>
</body>
有什麼不對呢?
有一個錯字,$ dhb!= $ dbh。除此之外,請確保它進入if行,並將所有PDO代碼包裝在try/catch塊中以檢查錯誤。 – aynber
啊,錯字現在被刪除!但是,它仍然不會改變分貝中的任何東西。我很確定它進入了if()語句。我在另一個文件中找到了相同的行,並且它工作正常。但該文件_adds_記錄而不是改變它。 – JeroenJK
此外,在Coda 2中,當我預覽.php文件時,他們都得到了這樣的錯誤:-----警告:PDO :: __構造():[2002]沒有這樣的文件或目錄(試圖連接通過致命錯誤:帶有消息'SQLSTATE [HY000] [2002]的未捕獲的異常'PDOException' - 在 - :21堆棧跟蹤:#中沒有這樣的文件或目錄0 - (21):PDO - > __ construct('mysql:host = loca ...','root','root')#1 {main} - 第21行-----,文件是唯一一個不這樣做的人。 – JeroenJK