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 8733857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:43:52+00:00 2026-06-13T09:43:52+00:00

Tables and Columns names cannot be bind using PDO ->bindParam(), but I am sure

  • 0

Tables and Columns names cannot be bind using PDO ->bindParam(), but I am sure more than one would love to be able to. It is a little late, but I wrote this earlier and so far it works. I am kind of new to php, and would like to know what you think and if it is safe.

$type = "defaultTableName";
$sortBy = "defaultColumnName";
$orderBy = "ASC";

//whitelisting unsafe input
if(isset($_GET['orderBy'])&&($_GET['orderBy']=="ASC"||$_GET['orderBy']=="DESC"))
    $orderBy = $_GET['orderBy'];
$tableNames = array("defaultTableName", "tableName2", "tableName3");
$unsafeType= $_GET['type']; <---unsafe input
$unsafeSortBy = $_GET['sortBy']; <---unsafe input

try {
    $pdo = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //if input is not valid this will use default table to render a table in html.
$stmt = $pdo->prepare("DESCRIBE $type");
$stmt->execute();
$table_fields = $stmt->fetchAll(PDO::FETCH_COLUMN);

 //Whitelisting user input against table names (will require table names updates)
    if (in_array($unsafeType, $tableNames)) {
    $stmt = $pdo->prepare("DESCRIBE $unsafeType");
    $stmt->execute();
    $table_fields = $stmt->fetchAll(PDO::FETCH_COLUMN);

 ///Whitelisting the column name to sort by against the description of the table.
        if (in_array($unsafeSortBy, $table_fields)) {
        $stmt = $pdo->prepare("SELECT * FROM $unsafeType ORDER BY $unsafeSortBy $orderBy");
    }
    else    {
        $stmt = $pdo->prepare("SELECT * FROM $type ORDER BY $sortBy $orderBy");
    }
} else {
    $stmt = $pdo->prepare("SELECT * FROM $type ORDER BY $sortBy $orderBy");
}
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
 }

Only problem I see is that you will need to add/delete/change the table name array when you change the tables. I have a small/medium application in mind, not very complex.

Note: I am also terrible editing in stackoverflow, so if you know a way to make it nicer go ahead and edit or let me know.

  • 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-06-13T09:43:53+00:00Added an answer on June 13, 2026 at 9:43 am

    No. It’s not safe. You’re directly placing user-submitted data into the query string. ANYTIME you do that you’re vulnerable to sql injection attacks.

    However, since you can’t use placeholders for those particular values, you’ll have to escape the data yourself with pdo::quote, e.g.

    $safeType = $pdo->quote($_GET['type']);
    

    just because it’s a table name or a sort-by clause value doesn’t mean it can’t be injected. ANY user data going into a string that’s not been quoted/escaped or inserted via placeholders is an attack vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database with many tables and one particular table has columns: name
How do I retrieve names of the columns of a table in MySQL using
How to retrieve data from two tables whose column names are the same?
How can I display the names of table columns in visual editor eclipse? I
given the two tables and values: Tables People- Columns: [ID]PK Name Field - Columns:
I made three tables. Table1=users .And the column names are userid (auto_increment) and username
I had made three tables. Table1=users .And the column names are userid (auto_increment) and
I have three tables, Name - columns Custpackingslipjour(table 1) - (deliverydate, dateclosed,, salesid) Inventrans
I have an old database (with terribly named tables and columns) and an entity
I have two tables: Agents with columns: agentid, name DocumentsRead with columns: agentid, document,

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.