Hi!
I want to know that how can i use buttons to update ( edit or delete) records from the php form directly into the database...
Thanks
Building on and : two practical patterns work well when query results are shown as an HTML table — either link each row to a dedicated edit page, or embed a small POST form per row for inline update/delete controls. Prefer POST for any change (never use a plain GET link for deletes), use prepared statements to avoid SQL injection, and include a CSRF token plus server-side validation and output escaping to avoid XSS. The snippets below show a safe, modern approach using PDO and a per-row form pattern.
HTML (per table row):
<tr>
<td><?php echo htmlspecialchars($row['name'], ENT_QUOTES); ?></td>
<td>
<form method="post" action="edit.php" style="display:inline">
<input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>">
<input type="hidden" name="csrf" value="<?php echo $_SESSION['csrf_token']; ?>">
<button name="action" value="edit">Edit</button>
</form>
<form method="post" action="edit.php" style="display:inline" onsubmit="return confirm('Delete this record?');">
<input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>">
<input type="hidden" name="csrf" value="<?php echo $_SESSION['csrf_token']; ?>">
<button name="action" value="delete">Delete</button>
</form>
</td>
</tr> Server-side handling (PDO, basic example):
<?php
// assume session started and $pdo is a PDO instance
$action = $_POST['action'] ?? '';
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if (!$id || !hash_equals($_SESSION['csrf_token'] ?? '', $_POST['csrf'] ?? '')) {
http_response_code(400);
exit;
}
if ($action === 'delete') {
$stmt = $pdo->prepare('DELETE FROM mytable WHERE id = :id');
$stmt->execute([':id' => $id]);
} elseif ($action === 'edit') {
$name = trim($_POST['name'] ?? '');
// validate $name before using
$stmt = $pdo->prepare('UPDATE mytable SET name = :name WHERE id = :id');
$stmt->execute([':name' => $name, ':id' => $id]);
} Troubleshooting / cautions: check rowCount() to confirm an update/delete actually affected a row, log DB errors, use transactions for multi-step changes, escape all output with htmlspecialchars(), and avoid the old mysql_* extension—use PDO or mysqli with prepared statements. For complex UIs, an edit page (as suggested) keeps validation simpler; inline forms are better for quick single-field edits.
Jump to Post— Josh Connerty 20Hi!
I want to know that how can i use buttons to update ( edit or delete) records from the php form directly into the database...
Thanks
Ok what you are trying to do it edit a table right...
If you want to display the row in the form …
Jump to Post— weblover 0can u make it more clear what u want so i can give u the help u need
Hi!
I want to know that how can i use buttons to update ( edit or delete) records from the php form directly into the database...
Thanks
Ok what you are trying to do it edit a table right...
If you want to display the row in the form for them to update or delete then I would advise this...
Create the form and echo the contents out.
Make sure you either have the id displayed in a text box or you have it in a hidden field.
You will need to submit buttons with two different values ( Update , Delete ) they should also have the same name and that should be it for the form.
When it gets to the document handling the request (the action on the form) we need to do something similar to this.
<?php
if( isset( $_POST['Update'] ) ) {
// Run your update query
} elseif( isset( $_POST['Delete'] ) ) {
// Delete from query
}
?> That would be how you structure the document. If you are unclear in any way on how to update or delete rows in a MySql table then please refer to the tutorials.
can u make it more clear what u want so i can give u the help u need
Ok what you are trying to do it edit a table right...
If you want to display the row in the form for them to update or delete then I would advise this...
Create the form and echo the contents out.
Make sure you either have the id displayed in a text box or you have it in a hidden field.
You will need to submit buttons with two different values ( Update , Delete ) they should also have the same name and that should be it for the form.
When it gets to the document handling the request (the action on the form) we need to do something similar to this.
<?php if( isset( $_POST['Update'] ) ) { // Run your update query } elseif( isset( $_POST['Delete'] ) ) { // Delete from query } ?>That would be how you structure the document. If you are unclear in any way on how to update or delete rows in a MySql table then please refer to the tutorials.
I have query results in the form of html table not in the form of text or hidden fields.
hi
there are different ways to do that but most easiest and simple is as follows
first of all you have to fetch records from database and apply a link of edit in front of each record like
id name operation
1 bzzbee edit
2 tena edit
apply a link of word edit
like
<a href='update-record.php?cmd=insert&id=<?=$myrecordid?>' >Edit</edit> this link will go on php form named as update-record.php
request the variable and apply this code
<?php
$cmd=$_REQUEST['cmd'];
$id=$_REQUEST['id'];
if( $cmd=='insert' )
{
$query="update mytable set name='jolly' where person_id='$id' ";
mysql_query($query);
}
?> Will this work for result shown in the formo of tables??????
Yes add a link next to the table row saying edit or something like that and link it to a form with the values in a text field so you can then update or delete the entry.
yes.. josh is right.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.