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

Related Questions

I have the following code: public function createNewGuide($userID,$guideName) { $sql = INSERT INTO myTable(name,
I have the following query to insert into a table BULK INSERT tblMain FROM
I have the following table: I want to insert values from the MySQL workbench.
I have the following function to create the SQL for an insert query: function
I have the following query: INSERT INTO table (a, b, c) VALUES (NOW(), NOW()
I have the following query in VBA: DoCmd.RunSQL INSERT INTO table (value) VALUES ('example')
I have the following sample of data to insert into tables(from parent to child,
I have the following code: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)
I have the following code: var myModule = (function($) { // Insert code here
I have following function which returns Table . create Function FN(@Str varchar(30)) returns @Names

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.