How to run INSERT query using Mysqli

  1. Running INSERT query with raw Mysqli
  2. INSERT query with a helper function
  3. Comments

It goes without saying that you must use prepared statements for any SQL query that would contain a PHP variable. Therefore, as usually the INSERT query makes little sense without variables, it should always run through a prepared statement. To do so:

Running INSERT query with raw Mysqli

Here is a simple example. First of all, make sure that there is a proper connection to mysqli. Then write a code like this (adding your own table and field names and variables):

$sql "INSERT INTO users (name, surname, sex) VALUES (?,?,?)";
$stmt$conn->prepare($sql);
$stmt->bind_param("sss"$name$surname$sex);
$stmt->execute();

And have your query executed without a single syntax error or SQL injection.

What is going on here?

$sql "INSERT INTO users (name, surname, sex) VALUES (?,?,?)";

Like it was said above, first we are writing an SQL query where all variables are substituted with question marks.

IMPORTANT! there should be no quotes around question marks, you are adding placeholders, not strings.

$stmt$conn->prepare($sql);

Then, the query is prepared. The idea is very smart. To avoid even a possibility of the SQL injection or a syntax error caused by the input data, the query and the data are sent to database server separately. So it goes on here: with prepare() we are sending the query to database server ahead. A special variable, a statement is created as a result. We would use this variable from now on.

$stmt->bind_param("sss"$name$surname$sex);

Then variables must be bound to the statement. The call consists of two parts - the string with types and the list of variables. With mysqli, you have to designate the type for each bound variable. It is represented by a single letter in the first parameter. The number of letters should be always equal to the number of variables. The possible types are

NOTE that you can almost always safely use "s" for any variable.

So you can tell now that "sss" means "there would be 3 variables, all of string type". And then, naturally, three variables obediently follow.

$stmt->execute();

Then the query finally gets executed. Means variables get sent to database server and the query is actually executed.

NOTE that you don't have to check the execution result. Given you have the proper connection code mentioned above, in case of error mysqli will raise an error automatically.

INSERT query with a helper function

As you may noted, the code is quite verbose. If you like to build a code like a Lego figure, with shining ranks of operators, you may keep it as is. If you, like me, hate useless repetitions and like to write concise and meaningful code, then there is a simple helper function. With it, the code will become two times shorter:

$sql "INSERT INTO users (name, surname, sex) VALUES (?,?,?)";
prepared_query($conn$sql, [$name$surname$sex]);

Here we are calling the helper function using a connection variable, an sql query and an array with variables. Simple, clean and safe!


Related articles: