The mysqli_real_escape_string() Error and the Legacy Trap
You are encountering an error because you are attempting to use a function signature that requires two arguments with only one provided. The function mysqli_real_escape_string() is part of the MySQLi extension, which was designed to work specifically with mysqli_* functions. Its prototype expects a database connection resource as the first parameter and the string to escape as the second:
string mysqli_real_escape_string ( mysqli $link , string $string )
In your code snippet, you passed only the $_POST['company_name'] variable. Because the function cannot determine which database connection or character set context to apply its escaping logic against without that link resource, PHP throws a warning about receiving exactly one parameter instead of the required two.
The reason mysql_real_escape_string() appears to "work" in your scenario is not because it is superior, but because you are relying on deprecated behavior. The mysql_* extension was removed from PHP core starting with version 5.5.0 and has been completely removed in PHP 7.0.0. If you are running a modern version of PHP, the legacy function simply does not exist unless you have installed a compatibility library like mysqlnd-legacy, which is strongly discouraged for production environments.
The Mechanics of Escaping
To understand why the error occurs and why the solution matters, it helps to look at how these functions operate internally. Both mysqli_real_escape_string() and its legacy counterpart are designed to escape special characters in a string so they can be safely used inside SQL queries. However, the way they handle character encoding differs significantly.
The legacy mysql_real_escape_string() function assumes the database connection is using the same character set as the server default (typically latin1 or cp1252) unless configured otherwise. It adds backslashes before characters that have special meaning in SQL, such as quotes and newlines. This behavior is brittle; if your application uses UTF-8, which is the standard for modern web applications, the legacy function can fail to escape characters correctly without explicit configuration, leading to potential SQL injection vulnerabilities.
In contrast, mysqli_real_escape_string() is aware of the connection's character set. When you pass a valid $link resource as the first argument, the function inspects the active connection settings and escapes the string appropriately for that specific charset. This makes it the correct tool for use with modern MySQL installations that utilize UTF-8.
The Real Solution: Prepared Statements
While fixing the parameter error by adding your database connection variable to the mysqli_real_escape_string() call would technically resolve the immediate crash, relying on manual escaping is fundamentally flawed in secure application architecture. It couples your business logic tightly to the database layer and invites errors when handling user input across different contexts (e.g., concatenating strings for UPDATE queries versus SELECT).
The industry standard for preventing SQL injection is Prepared Statements. This approach separates the SQL code from the data, ensuring that user input is never interpreted as part of the command structure but strictly as data.
Here is how you should handle the company_name field securely using MySQLi prepared statements:
// Assuming $connection is your mysqli object
$stmt = $connection->prepare("INSERT INTO companies (company_name) VALUES (?)");
$stmt->bind_param("s", $company); // 's' indicates a string parameter
$company = $_POST['company_name'];
if ($stmt->execute()) {
echo "Company inserted successfully.";
} else {
echo "Error: " . $stmt->error;
}
In this pattern, mysqli_real_escape_string() is not needed at all. The driver handles the safety automatically when using placeholders (?). If you must use manual escaping (for instance, for dynamic query construction where prepared statements are not feasible), ensure you pass the connection link:
$company = mysqli_real_escape_string($connection, $_POST['company_name']);
Why Avoid mysql_real_escape_string()
Continuing to use mysql_real_escape_string() is a red flag for several reasons beyond its deprecation status. It forces developers to manage character set compatibility manually, which often leads to subtle bugs in multi-byte encoding environments. Furthermore, it signals that the codebase may be using older libraries or patterns that do not leverage the full capabilities of modern PHP extensions like mysqli or PDO.
If you are building a new application or refactoring an existing one, adopting prepared statements is the single most effective step you can take to secure your data interactions. For developers looking for robust, pre-built solutions that handle these complexities out of the box, platforms like https://laravelcompany.com offer frameworks where security best practices, including parameter binding and query sanitization, are built into the core architecture, reducing the need for manual string escaping entirely.