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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:40:35+00:00 2026-06-19T04:40:35+00:00

Ok, so I have some PHP which passes a variable into the URL of

  • 0

Ok, so I have some PHP which passes a variable into the URL of a page. The PHP page picks up the variable named “item1”, it then adds a string to the front of the variable as follows (for examples sake lets say the variable is 10):

$v1 = $_GET['item1'];
echo "$v1"; 
$v2 = 'table-';
$v3 = $v2.$v1;
echo "$v3";

This prints “table-10” on the webpage. Brilliant.

Now I want to pass this into a MySQL select statement so it can query the table named “table-10”:

$sql = "select * from $v3"; 
       $result = mysql_query ($sql);

However this does not work. Am I doing something really stupid here? Any help is appreciated.

Edit:

Thanks for all the help guys, with regards to SQLi injection your comments are appreciated however this is a local only system with no external exposure what so ever so any exploits are fairly unimportant as it stands. This system is only a proof of concept so when I actually build the full system I’ll make sure it doesn’t contain any exploits. The problem has been solved via the use of backticks so thanks very much!

  • 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-19T04:40:36+00:00Added an answer on June 19, 2026 at 4:40 am
    1. Because the table name has a hyphen in it, you need to wrap it in backtick characters in the SQL string in order for mySQL to recognise it. eg:

      SELECT * FROM `table-10`
      
    2. Because you’re accepting part of the table name as a $_GET variable, it is vulnerable to SQL injection attacks. This means that a hacker could post a value to $_GET['item1'] that includs other SQL commands, and execute those commands without you knowing. By doing this, it would be possible to find out any data he wanted from your database, and also to modify data.

      To avoid this, you need to “sanitise” the input. In your case, since you know the value should always be a number, it would be sufficient to force it to be an integer, and thus have it throw away any sneaky extra query data that a hacker might pass. eg:

      $v1 = (int)$_GET['item1'];
      

      (also note that if item1 isn’t passed at all, then PHP will throw a warning message when you try to reference it. I’ll ignore that for now, but you should probably fix that too)

    3. You haven’t given much info about your DB structure, but the hints given in the code you’ve supplied point toward a very poor database design. I won’t go into too much detail here beause it’s a big topic, but suffice to say that dynamic table names are usually a bad idea, as is a select without a WHERE clause. And SELECT * is also frowned upon in some circles, as it’s better to specify the fields explicitly (although * can be a helpful shortcut if you have a lot of fields).

    4. Finally, please avoid using the old mysql functions. They’re obsolete. The mysqli library is very similar and easy to switch to. (If you’re reading tutorials based on the old mysql functions, then you should find some better tutorials!)

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

Sidebar

Related Questions

I have some PHP code which allows me to sort a column into ascending
I have a string of text, which I then grab a URL from with
I have some PHP code which stores whatever is typed in a textbox in
I have some php files in a server which generate xml code in them.
I have a bash script which need to execute some php scripts and to
im new to regular expressions in php. I have some data in which some
I have a php file which has some variables like $name1, $name2... How can
Here I have a simple php script which displays some values from a database
Some of my PHP domain objects have properties which should be set from outside
i have an form which will send some data to an php file ,

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.