Passing a Value Through Buttons to PHP Functions
The core issue you are facing stems from a fundamental misunderstanding of how HTML forms and server-side requests interact. In your current implementation, the <input type="button"> elements exist purely in the browser's Document Object Model. They do not inherently know which database row generated them, nor can they automatically "pass" data to a PHP function without an explicit mechanism to bridge that gap.
When you click the "Edit" or "Delete" button, the browser triggers the associated JavaScript event or submits the form containing those buttons. If your PHP script receives no parameters (like $_GET['id'] or $_POST['action']), it cannot determine which row to modify. The server effectively sees a generic request for an action on an unknown target.
The Problem with Plain Buttons
Your current code snippet generates a table where each row contains buttons named edit and delete. However, simply giving them a name attribute is insufficient for passing the Serial Number ($SN).
HTML attributes like value, name, or id are static. They do not dynamically bind to the loop variable $row['SN'] in a way that persists across form submissions unless you explicitly include that value in the request payload. Without this, your PHP function receives an empty context. You might think the button name carries the data, but the server only knows which button was clicked, not which row it belonged to, unless the row's unique identifier is sent along with the click event.
The Standard Approach: Hidden Inputs
To solve this, you must include the unique identifier (the Serial Number) in the form submission data. The most common and robust pattern is to use a hidden <input> field within each table row. Even though the user cannot see or edit this field, it travels with the form submission and tells the server exactly which record triggered the action.
Here is how you can modify your PHP loop to include these identifiers:
while ($row = mysql_fetch_array($query)) {
$SN = $row['SN'];
// ... other variables ...
echo "<tr>";
// Display data...
// Add a hidden input containing the Serial Number
echo "<input type='hidden' name='serial_number' value='" . htmlspecialchars($SN) . "'>";
// Now the buttons are part of this specific form context
echo "<td>
<input type='button' name='action' value='Edit' onclick=\"updateAction('edit', '" . htmlspecialchars($SN) . "')\">
<input type='button' name='action' value='Delete' onclick=\"updateAction('delete', '" . htmlspecialchars($SN) . "')\">
</td>";
echo "</tr>";
}
In this example, the hidden input ensures that when a request is eventually processed (either via form submit or an AJAX call), the server knows exactly which $SN was involved. Note that mysql_fetch_array is deprecated as of PHP 5.5 and removed in PHP 7.0. You must switch to mysqli or PDO for any production environment.
The Modern Approach: Laravel
If you are building an admin panel, relying on raw HTML tables and manual form handling often leads to spaghetti code that is hard to maintain. Frameworks like Laravel abstract this complexity by providing robust mechanisms to handle these interactions securely.
Laravel's Blade templating engine and Eloquent ORM allow you to pass data more cleanly without cluttering your HTML with hidden inputs for every single column. You can utilize the action method in your controller to route requests specifically to edit or delete logic, passing the ID via a query parameter or form field automatically.
For instance, instead of manually managing hidden fields and button names, you might generate forms that submit to routes like /admin/users/{id}/edit. The framework handles the routing and validation, ensuring that the id is passed securely through the URL or request body. This approach minimizes the risk of SQL injection and simplifies the logic required to fetch the specific row for editing.
Furthermore, Laravel provides helpers for rendering buttons that automatically include CSRF tokens (@csrf), which are essential for protecting your application against cross-site request forgery attacks. Manual implementation of these security measures is a common pitfall when transitioning from vanilla PHP to modern frameworks.
Handling the Request in PHP
Regardless of the method used to send the data, your PHP function must explicitly retrieve and validate the identifier before executing any database query. If you stick with the hidden input method shown earlier, your process_edit or process_delete function should look something like this:
public function edit() {
if (!isset($_POST['serial_number'])) {
return redirect()->back()->withErrors('No serial number provided.');
}
$sn = $_POST['serial_number'];
// Use prepared statements to prevent SQL injection
// Since mysql_* functions are removed, assume mysqli or PDO usage here:
// $stmt = $pdo->prepare("SELECT * FROM users WHERE SN = ?");
// $stmt->execute([$sn]);
}
The critical takeaway is that the data must be present in the $_POST or $_GET superglobals. The browser cannot magically know which row you want to edit without you explicitly telling it via a hidden field, a form action URL, or an AJAX payload. By embedding the Serial Number directly into the request context, you bridge the gap between the UI and the business logic, enabling precise operations on specific database rows.