What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a prepared INSERT statement to add a row to MySQL from PHP. The two common options are PDO, which uses prepare() and execute(), and MySQLi, which uses prepare(), bind_param(), and execute(). In both cases, put values in placeholders rather than joining user input into SQL.

Before you start

You need a MySQL database, a table with known columns, and PHP configured with the relevant driver: PDO_MYSQL for PDO or MySQLi for the MySQLi API. Replace the sample database name, credentials, table, columns, and PHP variables below with values from your application.

Both examples insert a name and email into a table called users. The table must have matching columns, and the PHP variables $name and $email must already contain the values you want to insert.

Method 1: Insert with PDO

PDO is PHP’s database abstraction interface; PDO_MYSQL is the driver that connects it to MySQL. Prepare the SQL template, then execute it with an array of values:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php
$pdo = new PDO(
    'mysql:host=localhost;dbname=example;charset=utf8mb4',
    'db_user',
    'db_password',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

$sql = 'INSERT INTO users (name, email) VALUES (:name, :email)';
$stmt = $pdo->prepare($sql);
$stmt->execute([
    'name' => $name,
    'email' => $email,
]);
  1. Construct a PDO connection using a MySQL DSN, username, and password. The example sets the connection character set to utf8mb4 and configures PDO to report errors as exceptions.
  2. Write the INSERT with explicit column names and named markers, such as :name and :email.
  3. Call prepare() on the connection, then pass the marker values to execute().

PDO also supports question-mark markers. Use one marker style consistently within a statement. The PDO MySQL driver enables emulated prepares by default, so using PDO’s prepared-statement interface does not by itself mean MySQL always prepares the statement on the server. See the PHP manual’s PDO::prepare and MySQL PDO Driver documentation.

Method 2: Insert with MySQLi

MySQLi is PHP’s MySQL-specific API and offers both object-oriented and procedural styles. This example uses the object-oriented style:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'db_user', 'db_password', 'example');
$mysqli->set_charset('utf8mb4');

$stmt = $mysqli->prepare('INSERT INTO users (name, email) VALUES (?, ?)');
$stmt->bind_param('ss', $name, $email);
$stmt->execute();
  1. Enable strict MySQLi error reporting and connect to the database.
  2. Set the connection character set, then prepare an INSERT statement with a question-mark marker for each value.
  3. Call bind_param() with a type string followed by the PHP variables. Here, ss means both values are strings.
  4. Call execute() to run the insert. When you need the number of affected rows, MySQLi provides mysqli_stmt_affected_rows().

With strict reporting enabled, MySQLi errors can raise a mysqli_sql_exception. Handle exceptions through your application’s normal error-handling path; avoid showing database credentials or internal error details to visitors. The PHP manual documents the MySQLi prepare operation and statement execution.

PDO or MySQLi: which should you use?

Consideration PDO MySQLi
Database scope Database abstraction interface; this example connects through PDO_MYSQL. MySQL-specific PHP API.
Placeholder style Named markers such as :name or positional question marks. Question-mark markers in the statement template.
Typical binding pattern Pass values to PDOStatement::execute(). Bind variables with bind_param(), then execute.
Good fit when Your application already uses PDO or needs its database abstraction interface. Your application already uses MySQLi or you want a MySQL-specific API.

Neither API is universally the better choice for every project. Prefer the one your codebase already uses unless you have a concrete reason to standardize on the other. Both support prepared INSERT workflows; the examples here do not establish a performance difference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep SQL structure separate from values

Placeholders represent data values, not identifiers. A marker can stand for a value in the VALUES list, but it cannot stand for a table name or column name. Keep identifiers in the SQL text. If your application must choose a table or column dynamically, validate that choice against an application-controlled allowlist before constructing the SQL. Do not concatenate untrusted input into the statement. The PHP manuals explain these marker limits for MySQLi and PDO prepared statements.

Common issues to check

  • Connection failure: Check that the correct PHP driver is enabled, the MySQL host and database name are correct, and the account credentials are valid.
  • Unknown column or table: Confirm the table and explicit column names match your database schema.
  • Parameter mismatch: Match each placeholder to one value; for MySQLi, ensure the type string has one type character per bound variable.
  • Character encoding problems: Keep the connection character set set to utf8mb4 as in these examples.
  • Errors are hard to diagnose: Configure exception or strict error reporting during development, then handle failures safely in the application rather than exposing details publicly.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Further reading

For a broader book-length treatment, Pearson lists PHP and MySQL Web Development, fifth edition, by Luke Welling and Laura Thomson. Pearson describes that edition as covering PHP 7 and MySQL 5.7, so its version scope is dated for current API specifics: Pearson’s book listing.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.