php update sql from form

php update sql from form

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Update SQL from Form: Fixing Variable Binding in Legacy Code

You have correctly identified that your ID variable is being populated, yet the update logic fails when using $_GET['id']. The root cause lies in how you are constructing the SQL query string in update_ac.php. You are defining the variable $id but then writing a hardcoded string "WHERE id='id'" instead of injecting the variable's value. This mismatch prevents the database from updating any row because it is literally searching for rows where the ID column equals the text string "id".

Additionally, your code relies on the deprecated mysql_* functions and uses raw string interpolation ($sql="SELECT ... WHERE id = '$id'"). While this might work in a local environment with trusted users, it is fundamentally flawed for production software. It leaves you exposed to SQL injection attacks and fails if input contains special characters like quotes or newlines.

Here is how to refactor your update_ac.php to handle the update correctly and securely.

Fixing the Query Logic

The immediate fix requires two steps in update_ac.php:

  1. Inject the Variable: You must concatenate the $id variable into the query string so the database knows which row to modify.
  2. Use Prepared Statements: To prevent SQL injection and handle special characters automatically, you should use a prepared statement with bound parameters. This separates the SQL logic from the data.

Here is the corrected implementation using mysqli:

<?php
// Connect to server and select database.
// Note: mysql_* functions are deprecated. Use mysqli instead.
$conn = new mysqli("localhost", "username", "password", "porting");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve the ID from the POST data hidden in the form
$id = $_POST['id'];

// Prepare the SQL statement
// Using placeholders (?) ensures special characters in inputs don't break the query
$stmt = $conn->prepare("UPDATE ports SET Customer=?, Number=?, Type=?, Comments=? WHERE id=?");

if (!$stmt) {
    die("Prepare failed: " . $stmt->error);
}

// Bind parameters
// 's' stands for string. Adjust based on your actual data types (e.g., 'i' for integer).
$stmt->bind_param("ssssi", 
    $_POST['Customer'], 
    $_POST['Number'], 
    $_POST['Type'], 
    $_POST['Comments'], 
    $id
);

// Execute the query
if ($stmt->execute()) {
    echo "Successful";
    echo "<br>";
    echo "<a href='list_records.php'>View result</a>";
} else {
    echo "ERROR: " . $stmt->error;
}

$stmt->close();
$conn->close();
?>

Understanding the $_GET vs $_POST Flow

In your current setup, update.php receives the ID via $_GET['id'] from the URL and then displays it in a form. The form itself submits back to update_ac.php, but notice the hidden input field: <input name="id" ...>.

When the form is submitted, this hidden field populates $_POST['id']. Your original script failed because update_ac.php was looking for $_GET['id'] (which wouldn't exist on a POST request) or failing to use the variable you just retrieved. Always ensure you are reading from $_POST when processing form submissions, even if the initial navigation used $_GET.

Why Legacy Code Needs Modernization

Your source code uses mysql_connect and mysql_select_db. These functions were deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0. If you upgrade your server version or switch to a newer PHP release, these scripts will stop working completely.

The modern standard is the mysqli extension (MySQL Improved) or PDO. They offer:

  • Prepared Statements: As shown above, this prevents SQL injection by treating user input strictly as data, never executable code.
  • Resource Management: Better handling of database connections and result sets.
  • Forward Compatibility: Scripts written with mysqli will run on current and future PHP versions without modification.

If you are maintaining legacy systems where migration is impossible, at minimum, you must validate every input string before inserting it into the query to avoid syntax errors caused by apostrophes in names or numbers. However, refactoring to mysqli is the only sustainable path forward for any project intended to outlive its initial author.

For further guidance on modernizing legacy PHP applications, check out resources like https://laravelcompany.com which often covers migration strategies and best practices for older codebases.

Our network