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

  • SEARCH
  • Home
  • 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 949833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:28:01+00:00 2026-05-15T23:28:01+00:00

I have the following insert function. Is it safe from a sql injection. If

  • 0

I have the following insert function. Is it safe from a sql injection. If it isn’t then how do I make it safe.

public function insert($postValues, $table){

    $dbh = $this->connect();

    try {
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $fields = implode(array_keys($postValues), ',');
        $values = "'".implode(array_values($postValues), "','")."'"; 
        $insertQuery = 'INSERT INTO '.$table.' ('.$fields.') VALUES (:'.$fields.')';

        $stmt = $dbh->prepare($insertQuery);

        foreach($postValues as $vals) {
            $stmt->execute($vals);
        }

        $message = $sucessMessage;
    }
    catch(PDOException $e){
        $message = $e->getMessage();
    }

    $dbh = null;

    return $message;
}

Thanks in Advance

  • 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-15T23:28:02+00:00Added an answer on May 15, 2026 at 11:28 pm

    If each column type is a PDO::PARAM_STR, then it is fairly simple to bind your parameters to unamed paramter markers using PDOStatement::execute. However, if the column types vary, then you need to specify the column type for each column when you bind to it with PDOStatement::bindParam.

    Accepting table and column names from what appears to be user input, is not a good idea. The query will fail if the table or column names are incorrect, but you need to be very careful to ensure that the table and column names are safe to use. The following example checks the table and column names against a whitelist, prior to executing any SQL:

    function insert($postValues, $table) {
        $dbh = $this->connect();
    
        // Create a simple whitelist of table and column names.
        $whitelist = array('my_table' => array('col1', 'col2', 'col3'));
    
        // Check if the table name exists in the whitelist.
        if(!array_key_exists($table, $whitelist)) {
            exit("$table is not a valid table name.\n");
        }
    
        // Count the number of columns that are found in the whitelist.
        $cols = count(
            array_intersect(
                $whitelist[$table],
                array_keys($postValues)));
    
        if($cols !== count($postValues)) {
            exit("One or more invalid column names have been supplied.\n");
        }
    
        // Create a comma separated list of column names.
        $columns = implode(', ', array_keys($postValues));
        // Create a comma separated list of unnamed placeholders.
        $params = implode(', ', array_fill(0, count($postValues), '?'));
        // Create a SQL statement.
        $sql = "INSERT INTO $table ($columns) VALUES ($params)";
    
        // Prepare the SQL statement.
        $stmt = $dbh->prepare($sql);
        // Bind the values to the statement, and execute it.
        return $stmt->execute(array_values($postValues));
    }
    
    echo insert(
        array(
            'col1' => 'value1',
            'col2' => 'value2',
            'col3' => 'value3'),
        'my_table');
    
    // 1
    
    echo insert(
        array(
            'col1' => 'value1',
            'col2' => 'value2',
            'col3' => 'value3'),
        'unsafe_table');
    
    // unsafe_table is not a valid table name.
    
    echo insert(
        array(
            'col1' => 'value1',
            'col2' => 'value2',
            'unsafe_col' => 'value3'),
        'my_table');
    
    // One or more invalid column names have been supplied.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following code: public function createNewGuide($userID,$guideName) { $sql = INSERT INTO myTable(name,
I have the following function to create the SQL for an insert query: function
I have the following code I use to insert form data into a single
I have the following code that should, when run, update a table of victims
I have the following SQL which gets a season for each day in a
If I have the following input (excluding quotes): The ancestral territorial imperatives of the
I have the following scenario. I have two collections, one has some items missing.
I have the following in my XSL which adds a xmlns to my XML.
Is it possible to put a function in an XML field? For example i
What's wrong with the following implementation of a binary search tree (BST) ? I

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.