June 7, 201412 yr I am trying to created an edit record page. On the page I have all the records pulled from the database with an edit button next to it. Below these records i have a form, which currently is being populated with the latest record, but what I want to achieve is when the edit button is clicked next to the database record, the details are populated into the from below. When the details are edited there is an Update button below and once clicked I want the record to be updated in the database. How can I achieve this? Note the JOIN LEFT as there are two tables being used (customer & services) I am trying to do this using PHP OOP, which is really new to me so my experience is limited. <?php error_reporting(0); require 'db/connect.php'; require 'functions/security.php'; // the stored records we want to display $records = array(); // check if form has data entered if (!empty($_POST)) { if (isset($_POST['first_name'], $_POST['last_name'], $_POST['website'], $_POST['start_date'], $_POST['service'])) { $first_name = trim($_POST['first_name']); $last_name = trim($_POST['last_name']); $website = trim($_POST['website']); $start_date = date('Y-m-d', strtotime($_POST['start_date'])); $service = ($_POST['service']); if (!empty($first_name) && !empty($last_name) && !empty($website) && !empty($start_date) && !empty($service)) { $insert = $db->prepare("INSERT INTO customer (first_name, last_name, website, start_date, service) VALUES (?, ?, ?, ?, ?)"); $insert->bind_param('ssssi', $first_name, $last_name, $website, $start_date, $service); if ($insert->execute()) { header('Location: index.php'); die(); } } } } // result of our database query if ($results = $db->query("SELECT customer.*, services.service_name as service FROM customer LEFT JOIN services ON customer.service = services.id")) { if ($results->num_rows) { while ($row = $results->fetch_object()) { $records[] = $row; } $results->free(); } } ?> <!DOCTYPE html> <html> <head> <title>add customer</title> </head> <body> <table> <tr> <td><a href="index.php">dashboard</a></td> <td><a href="add.php">add customer</a></td> <td><a href="">delete customer</a></td> <td><a href="edit.php">edit customer</a></td> </tr> </table> <h3>Customer</h3> <?php if (!count($records)) { echo 'No records'; } else { ?> <table> <thead> <tr> <th>First Name</th> <th>Last Name</th> <th>Website</th> <th>Start Date</th> <th>Service</th> <th>EDIT</th> </tr> </thead> <tbody> <?php foreach ($records as $r) { ?> <tr> <td><?php echo escape($r->first_name); ?></td> <td><?php echo escape($r->last_name); ?></td> <td><?php echo escape($r->website); ?></td> <td><?php echo escape($r->start_date); ?></td> <td><?php echo escape($r->service); ?></td> <td><button>edit</button></td> </tr> <?php } ?> </tbody> </table> <?php } ?> <hr> <form action="" method="post"> <div class="field"> <label for="get_first_name">First Name</label> <input type="text" name="get_first_name" id="get_first_name" autocomplete="off" value="<?php echo $r->first_name; ?>"> </div> <div class="field"> <label for="get_last_name">Last Name</label> <input type="text" name="get_last_name" id="get_last_name" autocomplete="off" value="<?php echo $r->last_name; ?>"> </div> <div class="field"> <label for="get_website">Website</label> <input type="text" name="get_website" id="get_website" autocomplete="off" value="<?php echo $r->website; ?>"> </div> <div class="field"> <label for="get_start_date">Start Date</label> <input type="text" name="get_start_date" id="get_start_date" autocomplete="off" value="<?php echo $r->start_date; ?>"> </div> <div class="field"> <label for="get_service">Service</label> <input type="text" name="get_service" id="get_service" autocomplete="off" value="<?php echo $r->service; ?>"> </div> <input type="submit" value="Update"> </form> </body> </html>
June 11, 201412 yr So if($_POST['update']){ $sql = "UPDATE `customers` SET `name` = '$escaped_name', `phone` = '$escaped_phone', ???? ???? ??? ??? WHERE `customer_id` = '$editid' "; Hope this is what you where after? T }
June 12, 201412 yr Don't forget to sanitise any inputs: <?php function cleanInput($input) { $search = array( '@<script[^>]*?>.*?</script>@si', // Strip out javascript '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments ); $output = preg_replace($search, '', $input); return $output; } function sanitize($input) { if (is_array($input)) { foreach($input as $var=>$val) { $output[$var] = sanitize($val); } } else { if (get_magic_quotes_gpc()) { $input = stripslashes($input); } $input = cleanInput($input); $output = mysql_real_escape_string($input); } return $output; ?> No you can do as above : $escaped_first_name = $_POST['get_first_name']; $sql = "UPDATE 'customers' SET 'name' = '$escaped_first_name' WHERE 'customer_id' = '$id'"; Edited June 12, 201412 yr by mantis
Create an account or sign in to comment