Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:41:34+00:00 2026-05-25T17:41:34+00:00

I’m trying to insert values in the contents table. It works fine if I

  • 0

I’m trying to insert values in the contents table. It works fine if I do not have a PHP variable inside VALUES. When I put the variable $type inside VALUES then this doesn’t work. What am I doing wrong?

$type = 'testing';
mysql_query("INSERT INTO contents (type, reporter, description) 
     VALUES($type, 'john', 'whatever')");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:41:35+00:00Added an answer on May 25, 2026 at 5:41 pm

    The rules of adding a PHP variable inside of any MySQL statement are plain and simple:

    1. Use prepared statements

    This rule covers 99% of queries and your query in particular. Any variable that represents an SQL data literal, (or, to put it simply – an SQL string, or a number) MUST be added through a prepared statement. No exceptions. A constant value can be put as is.

    This approach involves four basic steps

    • in your SQL statement, replace all variables with placeholders
    • prepare the resulting query
    • bind variables to placeholders
    • execute the query

    And here is how to do it with all popular PHP database drivers:

    Adding data literals using mysqli

    Starting from PHP 8.2 you can do the entire prepare/bind/execute sequence in one call:

    $type = 'testing';
    $reporter = "John O'Hara";
    $sql = "INSERT INTO contents (type,reporter,description) VALUES ('whatever',?,?)";
    $mysqli->execute_query($sql, [$reporter, $description]);
    

    If your PHP version is old, then prepare/bind/execute has to be done explicitly:

    $type = 'testing';
    $reporter = "John O'Hara";
    $sql = "INSERT INTO contents (type,reporter,description) VALUES ('whatever',?,?)";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param("ss", $reporter, $description);
    $stmt->execute();
    

    The code is a bit complicated but the detailed explanation of all these operators can be found in my article, How to run an INSERT query using Mysqli, as well as a solution that eases the process dramatically.

    For a SELECT query you can use the same method as above:

    $reporter = "John O'Hara";
    $result = $mysqli->execute_query("SELECT * FROM users WHERE name=?", [$reporter]);
    $row = $result->fetch_assoc(); // or while (...)
    

    but again, if your PHP version is old, you will need to go through prepare/bind/execute routine and also add a call to get_result() method, in order to get a familiar mysqli_result from which you can fetch the data the usual way:

    $reporter = "John O'Hara";
    $stmt = $mysqli->prepare("SELECT * FROM users WHERE name=?");
    $stmt->bind_param("s", $reporter);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc(); // or while (...)
    
    Adding data literals using PDO
    $type = 'testing';
    $reporter = "John O'Hara";
    $sql = "INSERT INTO contents (type,reporter,description) VALUES ('whatever',?,?)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$reporter, $description]);
    

    In PDO, we can have the bind and execute parts combined, which is very convenient. PDO also supports named placeholders which some find extremely convenient.

    2. Use white list filtering

    Any other query part, such as SQL keyword, table or a field name, or operator – must be filtered through a white list.

    Sometimes we have to add a variable that represents another part of a query, such as a keyword or an identifier (a database, table or a field name). It’s a rare case but it’s better to be prepared.

    In this case, your variable must be checked against a list of values explicitly written in your script. This is explained in my other article, Adding a field name in the ORDER BY clause based on the user’s choice:

    Unfortunately, PDO has no placeholder for identifiers (table and field names), therefore a developer must filter them out manually. Such a filter is often called a "white list" (where we only list allowed values) as opposed to a "black-list" where we list disallowed values.

    So we have to explicitly list all possible variants in the PHP code and then choose from them.

    Here is an example:

    $orderby = $_GET['orderby'] ?: "name"; // set the default value
    $allowed = ["name","price","qty"]; // the white list of allowed field names
    $key = array_search($orderby, $allowed, true); // see if we have such a name
    if ($key === false) { 
        throw new InvalidArgumentException("Invalid field name"); 
    }
    

    Exactly the same approach should be used for the direction,

    $direction = $_GET['direction'] ?: "ASC";
    $allowed = ["ASC","DESC"];
    $key = array_search($direction, $allowed, true);
    if ($key === false) { 
        throw new InvalidArgumentException("Invalid ORDER BY direction"); 
    }
    

    After such a code, both $direction and $orderby variables can be safely put in the SQL query, as they are either equal to one of the allowed variants or there will be an error thrown.

    The last thing to mention about identifiers, they must be also formatted according to the particular database syntax. For MySQL it should be backtick characters around the identifier. So the final query string for our order by example would be

    $query = "SELECT * FROM `table` ORDER BY `$orderby` $direction";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
this is what i have right now Drawing an RSS feed into the php,
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.