gpt4 book ai didi

php - 使用 PHP/PDO 和 mySQL 更新数据库记录

转载 作者:行者123 更新时间:2023-11-29 00:25:27 25 4
gpt4 key购买 nike

我有一个数据库“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>

这是怎么回事?

最佳答案

我使用您的代码设置了快速测试页面。我发现有两件事不能正常工作。

  1. 初始表单 - 表单中禁用了“naam”字段。这意味着该参数没有进入 Wijzig.php 页面。由于“naam”始终为空——因此等于“”——它不会通过你的 if 语句。为了让它工作,我在第一个表单中重新启用了“naam”字段。
  2. UPDATE 语句中的参数 - 在您的 UPDATE 语句中,它试图访问名为“:telefoonnummer”的参数,但在参数中使用了“:nummer”。这会导致 PDO 在执行时抛出异常。

快速说明 - 我向 wijzig.php 添加了一个 echo 语句,以便成功运行会产生某种可见的结果。

有了这个,我将这两个文件更新成这样:

索引.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>

当针对现有记录运行时,它会产生以下输出:

Number of records changed: 1

关于php - 使用 PHP/PDO 和 mySQL 更新数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19144840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com