6. Edit data (Update)
Editing data has a similar flow to adding data. After we click edit from index, we’ll be directed to the edit form.
The edit page works similar to the add page in that it tests if the form data has been submitted, validates that data, and then saves it to the database. The differences are, when you open the form it is already fill in with data from the database and when you save that data (assuming you will be making changes), an existing record, in the database, is updated.
Create the file: edit.php. Paste in the following code:
<?php include('connect.php'); include('head.php'); // if form is not empty if (!empty($_POST)) { // retrieving data from form $id = $_POST['id']; $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $notes = $_POST['notes']; // validating the data is not empty $valid = true; if (!$name) { echo '<script> alert("Please enter valid name"); </script>'; $valid = false; } if (!$email) { echo '<script> alert("Please enter valid email"); </script>'; $valid = false; } // if valid, edit the data based on id if ($valid) { $sql = 'UPDATE contacts SET name=?, email=?, phone=?, notes=? WHERE id=?'; $row = $connection->prepare($sql); $row->execute([$name, $email, $phone, $notes, $id]); // redirect to index echo '<script> alert("Contact edited"); window.location="index.php" </script>'; } } // retrieving the data by id $id = $_GET['id']; $sql = "SELECT * FROM contacts WHERE id=?"; $row = $connection->prepare($sql); $row->execute([$id]); $contact = $row->fetch(); ?> <h1>Edit Contact - <?php echo $contact['name']; ?></h1> <!-- the form is prefilled with the data retrieved --> <form action="edit.php?id=<?php echo $contact['id']; ?>" method="post"> <input type="hidden" name="id" id="id"value="<?php echo $contact['id']; ?>"> <label for="name">Name</label> <input type="text" name="name" placeholder="John Doe" id="name" value="<?php echo $contact['name']; ?>"> <br> <label for="email">Email</label> <input type="email" name="email" placeholder="[email protected]" id="email" value="<?php echo $contact['email']; ?>"> <br> <label for="phone">Phone</label> <input type="tel" name="phone" placeholder="928174823" id="phone" value="<?php echo $contact['phone']; ?>"> <br> <label for="notes">Notes</label> <input type="text" name="notes" placeholder="My Friend" id="notes" value="<?php echo $contact['notes']; ?>"> <br> <input type="submit" value="Update"> </form> <?php include('foot.php') ?>
This step will have a similar result as adding a data. You’ll also see a form, confirmation and index page with you edited data.
Break it Down
The code, on this page, works very much the same as the code for adding a record. I will only cover the difference here which is, this code updates the database instead of creating a new record
Get our record from the database so we can prefill our form.
// retrieving the data by id $id = $_GET['id']; $sql = "SELECT * FROM contacts WHERE id=?"; $row = $connection->prepare($sql); $row->execute([$id]); $contact = $row->fetch();
Once the form is modified and submitted, we update the existing record, changing the data to the new data.
// if form is not empty if(!empty($_POST)){ // retrieving data from form $id = $_POST['id']; ... // if valid, edit the data based on id if ($valid) { $sql = 'UPDATE contacts SET name=?, email=?, phone=?, notes=? WHERE id=?'; $row = $connection->prepare($sql); $row->execute([$name, $email, $phone, $notes, $id]); // redirect to index echo '<script> alert("Contact edited"); window.location="index.php" </script>'; } }