Hello so i'm learning php and basiclly i want that any field that has been already added to the database to be "editable"
Let me make it more clear with an example
My from looks like this (ver basic)
Firstname [input field]
Lastname [input field]
ZIP Code [input field]
send button
<form method="post" action="">
<table>
<tr>
<td>Vorname</td>
<td><input type="text" name="firstname"></td>
</tr>
<tr>
<td>Nachname</td>
<td><input type="text" name="lastname"></td>
</tr>
<tr>
<td>PLZ</td>
<td><input type="number" name="plz"></td>
</tr>
</table>
<button type="submit" name="send">Send</button>
</form>
When the send button is pressed the output is beeing shown in a table below like so
|---------------------|------------------|------------------|
| Firstname | Lastname | Zip Code |
|---------------------|------------------|------------------|
| Tomas | Möller | 123123 |
|---------------------|------------------|------------------|
and next to those to rows i still have these two (which didn't fit)
|---------------------|------------------|
| Update | Delete |
|---------------------|------------------|
| update | delete |
|---------------------|------------------|
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">PLZ</th>
<th scope="col">Delete</th>
<th scope="col">Update</th>
</tr>
</thead>
<tbody>
<?php
/**
* @var $contact ContactDto
*/
$contacts = $contactRepository->findAll();
foreach ($contacts as $contact) {
?>
<tr>
<th scope="row"><?php echo $contact->getId()?></th>
<td><?php echo $contact->getFirstname() ?></td>
<td><?php echo $contact->getLastname() ?></td>
<td><?php echo $contact->getPlz() ?></td>
<td><a href=delete.php?id=<?php echo (int)$contact->getId()?> >Delete</a></td>
<td><a href=update.php?id=<?php echo (int)$contact->getId()?> >Update</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
So basicaly each row has this own update and delte button.
Here is my problem..im not sure how to do this the right way.
I want when the update is clicked, the value from to fields "go back" to the top field and when send is pressed again the values are updated.
Here is my query
public function update(ContactDto $contactDto) {
$stmt = $this->pdo->prepare("UPDATE contacts SET firstname=:firstname,
lastname=:lastname,
plz=:plz
WHERE id=:id");
$stmt->bindValue(':firstname', $contactDto->getFirstname(), PDO::PARAM_STR);
$stmt->bindValue(':lastname', $contactDto->getLastname(), PDO::PARAM_STR);
$stmt->bindValue(':plz', $contactDto->getPlz(), PDO::PARAM_INT);
$stmt->bindValue(':id', $contactDto->getId(), PDO::PARAM_INT);
$stmt->execute();
}
I did try and make a new update.php file where the data is send, and the fields get filled like so (but that didnt quite work)
this is my update.php file
$contactRepository = new ContactRepository(
DatabaseConnection::getConnection()
);
$contactDto = new ContactDto();
$contactDto->setId($_GET['id']);
$contactDto->setFirstName($_POST['firstname']);
$contactDto->setLastName($_POST['lastname']);
$contactDto->setPlz($_POST['plz']);
$contactRepository->update($contactDto);
?>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $contactDto->getId(); ?>" />
<?PHP
?>
<table>
<tr>
<th>First Name: </th>
<td>
<input type="text" id="vorname" name="firstname" placeholder="Dein Vorname" size="35" value="<?php echo $contactDto->getFirstName(); ?>">
</td>
</tr>
<tr>
<th>Last Name: </th>
<td>
<input type="text" id="nachname" name="lastname" placeholder="Dein Nachname" size="35" value="<?php echo $contactDto->getLastName(); ?>">
</td>
</tr>
</table>
<input type="submit" name="save" value="Update" >
Which leaves me with..
>
Notice: Undefined index: firstname in C:\xampp\htdocs\test\update.php on line >14
Notice: Undefined index: lastname in C:\xampp\htdocs\test\update.php on line >15
Notice: Undefined index: plz in C:\xampp\htdocs\test\update.php on >line 16