[英]Updating database record with PHP/PDO and mySQL
我有一个数据库“ Telefoon”和一个表“ Telefoonnummers”。 该表具有“ naam”,“ number”和“ mobiel”列
我正在尝试制作一个.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>
这有什么问题?
我使用您的代码设置了快速测试页。 我发现有两件事不能正常工作。
快速说明 -我在wijzig.php中添加了echo语句,以便成功运行将产生某种可见的结果。
这样,我将两个文件更新为:
的index.php
<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
);
echo $record_name;
$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'/></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>
wijzig.php
<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";
$dbh = 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( ":nummer", $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 );
echo "Number of records changed: ".$sthCheck -> execute();
}
?>
</body>
对现有记录运行时,将产生以下输出:
记录数更改:1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.