Editing MySQL table row data by PHP & Ajax

Current approach: validate the whole row before updating

When several fields are editable, validate every submitted value first and update the row with one prepared statement. Do not build column values directly into SQL.

<?php
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
$mark = filter_input(INPUT_POST, 'mark', FILTER_VALIDATE_INT);
$name = trim((string) ($_POST['name'] ?? ''));
$class = trim((string) ($_POST['class'] ?? ''));
$sex = $_POST['sex'] ?? '';

if (!$id || $mark === false || $mark < 0 || $mark > 100 ||
    $name === '' || $class === '' || !in_array($sex, ['male', 'female'], true)) {
    http_response_code(422);
    exit('Invalid record data');
}

$stmt = $pdo->prepare(
    'UPDATE student SET name = :name, class = :class, mark = :mark, sex = :sex WHERE id = :id'
);
$stmt->execute([
    'name' => $name,
    'class' => $class,
    'mark' => $mark,
    'sex' => $sex,
    'id' => $id,
]);
?>

Return a small JSON response for the browser and escape values when rendering them back into HTML. Authentication, authorization and CSRF checks belong before the database update in a production application.

We can display rows of records and give user an option to edit and update any record. This way user can edit any data presented in a tabular format.

This tutorial is an extension of Editing single data of any record tutorial presented in Part 1. For better understanding it is highly recommended to read Part 1 of this tutorial before reading this.

Part I : Update single data of a record


Editing a complete row in the browser

Give each displayed field a predictable id such as name_6 or mark_6. When the user clicks Edit, read the current values with textContent, create form controls with DOM methods, and avoid rebuilding the row with innerHTML.

const markNode = document.getElementById('mark_' + id);
const input = document.createElement('input');
input.type = 'text';
input.value = markNode.textContent.trim();

The same pattern can create inputs for name and class and radio buttons for sex. On Update, send the complete row to the PHP processor using POST.

const body = new URLSearchParams({
    id, mark, name, class: classValue, sex
});

const response = await fetch('display-record-edit2-ajaxck.php', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
    body
});

Demo of editing all columns of a record

Validate and update the row in PHP

The processor should accept POST only, validate every field, then execute one prepared UPDATE. Return JSON so the browser can update the row without inserting server-generated HTML.

<?php
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
$mark = filter_input(INPUT_POST, 'mark', FILTER_VALIDATE_INT);
$name = trim((string)($_POST['name'] ?? ''));
$class = trim((string)($_POST['class'] ?? ''));
$sex = (string)($_POST['sex'] ?? '');

$stmt = $pdo->prepare(
    'UPDATE student SET name=:name, class=:class, mark=:mark, sex=:sex WHERE id=:id'
);
$stmt->execute([
    'name' => $name, 'class' => $class, 'mark' => $mark,
    'sex' => $sex, 'id' => $id
]);
?>

In production, add authentication, authorization and CSRF protection before the UPDATE.

Displaying message and updated record.

The JavaScript reads the JSON response, places returned values into the row using textContent, restores the Edit button, and shows a short status message. Errors are shown without inserting server-generated HTML.

Part I : Edit single column of a record


download script


Subscribe to our YouTube Channel here



plus2net.com







Antonis

30-12-2014

Nice very helpfull.
Jonathan Dusza

10-06-2015

Nice tutorial. Did you have information on using a dropdown list?
skechav

12-09-2015

Years the quality of your content still rocks!!! Rarely I do find such an great explanation of the code presented and its functionality...Many thanks!




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer