sjthetechguy Posted June 22, 2023 Share Posted June 22, 2023 Hi, I have this code which adds a record in databse. I have created a firms_edit.php and I am not sure what changes i make on this code which will edit the database entry using the id <?php include("./includes/functions_security.php"); include("./includes/functions_and_variables.php"); include("./includes/header-and-navigation.php"); ?> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <div class="card"> <div class="card-header"> <h5>Add Firm</h5> </div> <form class="form theme-form" method="POST" action="" autocomplete="off"> <div class="card-body"> <div class="row"> <div class="col"> <div class="mb-3 row"> <label class="col-sm-3 col-form-label">Legal Business Name</label> <div class="col-sm-9"> <input class="form-control" type="text" name="addfirm_legal_name" required> </div> </div> <div class="mb-3 row"> <label class="col-sm-3 col-form-label">Business Name</label> <div class="col-sm-9"> <input class="form-control" type="text" name="addfirm_business_name" required> </div> </div> <div class="mb-3 row"> <label class="col-sm-3 col-form-label">Address</label> <div class="col-sm-9"> <input class="form-control" type="text" name="addfirm_address" required> </div> </div> <div class="mb-3 row"> <label class="col-sm-3 col-form-label">City</label> <div class="col-sm-9"> <input class="form-control" type="text" name="addfirm_city" required> </div> </div> <div class="mb-3 row"> <label class="col-sm-3 col-form-label">State</label> <div class="col-sm-9"> <input class="form-control" type="text" name="addfirm_state" required> </div> </div> <div class="mb-3 row"> <label class="col-sm-3 col-form-label">PIN</label> <div class="col-sm-9"> <input class="form-control" type="text" name="addfirm_pin" required> </div> </div> <div class="mb-3 row"> <label class="col-sm-3 col-form-label">Country</label> <div class="col-sm-9"> <input class="form-control" type="text" name="addfirm_country" required> </div> </div> <div class="mb-3 row"> <label class="col-sm-3 col-form-label">Phone</label> <div class="col-sm-9"> <input class="form-control" type="text" name="addfirm_phone" required> </div> </div> </div> </div> </div> <br> <div class="card-footer text-end"> <div class="col-sm-9 offset-sm-3"> <button class="btn btn-primary" type="submit" name="submitaddfirm">Submit</button> <input class="btn btn-light" type="reset" value="Cancel"> </div> </div> </form> </div> </div> </div> </div> <?php // 1. Below PHP code adds the firm to the database ?> <?php $addfirmsuccessfully_alert = ""; $addfirmunsuccessful_alert = ""; if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Check if the form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve form data $addfirm_legalname = $_POST['addfirm_legal_name']; $addfirm_businessname = $_POST['addfirm_business_name']; $addfirm_address = $_POST['addfirm_address']; $addfirm_city = $_POST['addfirm_city']; $addfirm_state = $_POST['addfirm_state']; $addfirm_pin = $_POST['addfirm_pin']; $addfirm_country = $_POST['addfirm_country']; $addfirm_phone = $_POST['addfirm_phone']; // Prepare the SQL query $sqladdfirm = "INSERT INTO firms (firm_legalname, firmbusinessname, firmbusiness_addresss, firmbusiness_city, firmbusiness_state, firmbusiness_pin, firmbusiness_phone, firmbusiness_country) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; $stmtaddfirm = $conn->prepare($sqladdfirm); $stmtaddfirm->bind_param("ssssssss", $addfirm_legalname, $addfirm_businessname, $addfirm_address, $addfirm_city, $addfirm_state, $addfirm_pin, $addfirm_phone, $addfirm_country); // Execute the query if ($stmtaddfirm->execute()) { $addfirmsuccessfully_alert = "Firm added successfully"; } else { $addfirmunsuccessful_alert = "Error: " . $stmtaddfirm->error; } } ?> <?php include("./includes/footer.php"); include("./includes/scripts.php"); ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted June 22, 2023 Share Posted June 22, 2023 Include the ID with the form, either as a query parameter or as a form field. Pre-fill the fields with the existing data. Change your PHP code to do an UPDATE instead of an INSERT. And, obviously, change everything that says "add". Quote Link to comment Share on other sites More sharing options...
LeonLatex Posted June 22, 2023 Share Posted June 22, 2023 (edited) I used an hour on this. I am not so good at this, so I had to use some extra minutes on it. Hope I am right and you understand it. To edit a database entry using the id, you need to retrieve the id of the record to be edited, and then update the corresponding fields in the database with the new values. Here are the changes you can make to the existing code to enable editing of the database entry using the id: Update the form action to point to the PHP file that will handle the form submission, and pass the id of the record to be edited as a query parameter. For example: <form class="form theme-form" method="POST" action="edit_firm.php?id=<?php echo $firm_id; ?>" autocomplete="off"> Here, edit_firm.php is the PHP file that will handle the form submission, and $firm_id is the id of the record to be edited. Retrieve the id of the record to be edited from the query parameter in the URL. For example: if (isset($_GET['id'])) { $firm_id = $_GET['id']; } else { // handle error if id is not present in URL } Update the SQL query to use the UPDATE statement instead of INSERT, and set the values of the fields to be updated using the form data. For example: $sqleditfirm = "UPDATE firms SET firm_legalname=?, firmbusinessname=?, firmbusiness_addresss=?, firmbusiness_city=?, firmbusiness_state=?, firmbusiness_pin=?, firmbusiness_phone=?, firmbusiness_country=? WHERE id=?"; $stmtditfirm = $conn->prepare($sqleditfirm); $stmtditfirm->bind_param("ssssssssi", $addfirm_legalname, $addfirm_businessname, $addfirm_address, $addfirm_city, $addfirm_state, $addfirm_pin, $addfirm_phone, $addfirm_country, $firm_id); Here, id is the name of the primary key column in the firms table. Retrieve the existing values of the fields for the record to be edited from the database using the id, and prepopulate the form fields with these values. For example: // Retrieve existing values of the fields for the record to be edited $sqlgetfirm = "SELECT * FROM firms WHERE id=?"; $stmtgetfirm = $conn->prepare($sqlgetfirm); $stmtgetfirm->bind_param("i", $firm_id); $stmtgetfirm->execute(); $resultgetfirm = $stmtgetfirm->get_result(); $rowgetfirm = $resultgetfirm->fetch_assoc(); // Prepopulate the form fields with existing values $addfirm_legalname = $rowgetfirm['firm_legalname']; $addfirm_businessname = $rowgetfirm['firmbusinessname']; $addfirm_address = $rowgetfirm['firmbusiness_addresss']; $addfirm_city = $rowgetfirm['firmbusiness_city']; $addfirm_state = $rowgetfirm['firmbusiness_state']; $addfirm_pin = $rowgetfirm['firmbusiness_pin']; $addfirm_country = $rowgetfirm['firmbusiness_country']; $addfirm_phone = $rowgetfirm['firmbusiness_phone']; Note that you also need to handle any errors that may occur during the editing process. For example, you can display an error message if the record with the specified id is not found in the database. Edited June 22, 2023 by LeonLatex Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.