I have a database "Telefoon" with a table "Telefoonnummers". The table has the columns "naam", "number" and "mobiel"
I am trying to make a .php page where I can update a record. I am pretty sure this is right, but somehow nothing changes.
Here you fill in the updated records, and when you click the button it goes to the next page which updates it.
<!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>
This is the next page which actually changes (at least that's what I want) the record:
<!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>
What is wrong with this?
I set up quick test pages using your code. I found two things that weren't working correctly.
Quick Note - I added an echo statement to wijzig.php so that a successful run would produce a visible result of some sort.
With that, I have updated the two files to be like such:
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>
When run against an existing record, it produces the following output:
Number of records changed: 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.