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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:10:33+00:00 2026-05-22T02:10:33+00:00

I just had a friend of mine look over some code I’m using to

  • 0

I just had a friend of mine look over some code I’m using to get data from my database, and he tells me it’s very unsecure and that SQL injection is serious shit.

Here’s the code I’m using now:

$id = $_GET['id'];
$result = mysql_query("SELECT * FROM news WHERE id = $id") or die("err0r");

He tells me that the solution is to change that code into:

$id = intval($_GET['id']);
$result = mysql_query("SELECT * FROM news WHERE id = $id") or die("err0r");

My code somehow (according to my friend) makes any user able to edit content in my database:

http://mydomain.com/?p=news&id=38;DROP TABLE tablename;

Could someone explain exactly what he means?

Thank you and have a pleasant day

  • 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-22T02:10:34+00:00Added an answer on May 22, 2026 at 2:10 am

    Consider what would happen if someone posted http://www.mydomain.com?id=1; DROP TABLE news

    Older versions of PHP tried to automatically protect against this sort of thing by automatically escaping all the input variables; ie by adding slashes to any quote characters in the input so that they wouldn’t break a SQL query. This is no longer the default in current versions of PHP since it caused a lot of other issues.

    However in your case, you haven’t even quoted the variable in the SQL query, so even that protection wouldn’t have helped you since a hacker wouldn’t have needed to include any quotes to hack you.

    The intval() solution will indeed help you in this specific case, but it won’t help in others (eg if you need to handle a string variable).

    The correct solution:

    1. You should use the mysql_real_escape_string() function on all variables that are to be passed into the SQL query to prevent any possible hack attacks, like so:

      $escaped_id = mysql_real_escape_string($id);
      
    2. You should enclose all variables in the SQL string in quotes, like this:

      $result = mysql_query("SELECT * FROM news WHERE id = '$escaped_id'");
      

    By the way… This comic strip is a very popular thing to link to to demonstrate the problem. 😉

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

Sidebar

Related Questions

Just had a discussion at work about the merits of using PostgreSQL over MySQL
I just got a source code file from a friend. The file was created
Ok, initially this was just a joke we had with a friend of mine,
Recently a friend of mine had gone from a high level NOC position to
I just had some basic php questions to further my understanding as I learn,
I've just cleaned up a friend's e-commerce website. The hosting company had detected and
A friend of mine has written an irc bot, and has the code on
I Was explaing some module to one of my friend and i had to
I was recently asked by a friend of mine who's just starting to play
I was wondering what the best way of storing friend relationship data using MongoDB

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.