Update query PHP MySQL

Update query PHP MySQL

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

Stefan Bogdanescu

Founder & Senior Architect

Update Query PHP MySQL: Debugging a Broken Form Submission

The immediate culprit in your editblogscript.php file is that you are executing SQL queries without using prepared statements. In the code snippet provided, variables like $udcontent, $udtitle, and $id are concatenated directly into the query string. This approach fails for two critical reasons: syntax errors caused by special characters and severe security vulnerabilities.

Handling Special Characters

When a user submits a title or message containing a single quote (apostrophe), the SQL parser breaks because it expects the string to end there. For example, if udtitle is set to It's a beautiful day, the resulting query becomes:

UPDATE blogEntry SET content = It's a beautiful day, title = It's a beautiful day WHERE id = 5

The database interprets the first ' as the end of the string, then sees a beautiful day, title as invalid syntax. Your query simply fails silently or throws an error depending on the MySQL mode.

To fix this, you must escape the user input before inserting it into the SQL command. The legacy mysql_* extension provides a function called mysql_real_escape_string(). You need to wrap every variable in your update clause with this function:

mysql_query("UPDATE blogEntry SET content = '". mysql_real_escape_string($udcontent) ."', title = '" . mysql_real_escape_string($udtitle) . "' WHERE id = " . (int)$id);

Notice the casting of $id to an integer (int) as well. While less likely to cause a syntax error than strings, ensuring it is treated strictly as a number prevents injection attacks where a user might try to pass a string like 5; DROP TABLE blogEntry; --.

The Security Hole: SQL Injection

Beyond syntax errors, the concatenation method exposes your application to SQL injection. If a malicious actor submits a username or content field containing 1 OR 1=1, they could potentially update every row in the table or delete your database entirely.

The legacy mysql_* extension is deprecated as of PHP 5.5 and removed in PHP 7.0 because it cannot prevent these attacks without manual escaping. You should migrate to MySQLi (MySQL Improved) or PDO immediately. These extensions support prepared statements, which separate the SQL logic from the data values entirely.

Here is how you would refactor editblogscript.php using MySQLi:

<?php
// Establish connection
$conn = new mysqli("localhost", "root", "", "blogass");

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

if (isset($_POST['edit'])) {
    $id = $_POST['id'];
    $udtitle = $_POST['udtitle'];
    $udcontent = $_POST['udcontent'];

    // Prepare the statement
    $stmt = $conn->prepare("UPDATE blogEntry SET content=?, title=? WHERE id=?");
    
    // Bind parameters ('s' for string, 'i' for integer)
    // This ensures data is treated strictly as data, not code
    $stmt->bind_param("ssi", $udcontent, $udtitle, $id);

    if ($stmt->execute()) {
        echo "Record updated successfully.";
    } else {
        echo "Update failed: " . $stmt->error;
    }

    $stmt->close();
}

$conn->close();
header( 'Location: index.php' );
?>

Preventing Cross-Site Scripting (XSS)

Fixing the database update is only half the battle. Your HTML form displays user data directly back to the browser using echo. If a user saves an entry with <script>alert('hacked')</script>, and your script outputs that content without escaping it, any visitor to that blog post will execute that script in their browser.

You must sanitize output when displaying data. In your retrieval script (index.php or wherever you fetch the data), wrap the output of $content and $title with htmlspecialchars():

echo htmlspecialchars($content, ENT_QUOTES, 'UTF-8');
echo htmlspecialchars($title, ENT_QUOTES, 'UTF-8');

This converts special characters like < and > into their HTML entity equivalents (&lt; and &gt;), rendering the script tags harmless text.

Migration Path

You are currently using the mysql_* functions, which have been removed from core PHP distributions for years. Continuing to use them will eventually lead to fatal errors if you upgrade your PHP version. The industry standard has moved toward PDO or MySQLi for over a decade.

If you are building new features or refactoring existing code, consider adopting Laravel. It provides robust database abstraction layers and built-in protections against these common pitfalls. For more information on modern PHP development practices, you can read about the transition to secure frameworks at https://laravelcompany.com.

By implementing prepared statements and escaping output, your application will handle special characters correctly, remain secure against injection attacks, and survive future PHP version upgrades.

Our network