Help needed regarding PHP query

Discussion in 'PHP' started by hassanahmad2, Sep 4, 2009.

  1. #1
    Hello everyone,

    I just checked my website for sql vulnerabilities. The link of the page is like this:
    www.example.com/stories.php?id=4

    And the code is something like this:
    $id = $_GET["id"];
    if ($id != "") {
    $r = mysql_query("SELECT * FROM `stories` WHERE id = '$id'");
    }
    ...

    Now when I enter 4' instead of 4 for the id, it gives me an sql syntax error. As it should.
    But when I upload it to the server then it does not give me any error even though it breaks the sql syntax.

    Can someone please explain what is happening?

    Thanks in advance,
    Hassan
     
    hassanahmad2, Sep 4, 2009 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Maybe error reporting is turned off in your server

    Anyway your query is insecure. Try this:

    
    $id = $_GET["id"];
    if ($id != "" && is_numeric($id))) {
    $r = mysql_query("SELECT * FROM `stories` WHERE id = '".mysql_real_escape_string($id)."'");
    }
    
    PHP:
     
    ads2help, Sep 4, 2009 IP
  3. hassanahmad2

    hassanahmad2 Active Member

    Messages:
    243
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #3
    hassanahmad2, Sep 4, 2009 IP
  4. yuvrajm

    yuvrajm Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    have given the correct script... looks like something is missing here...
     
    yuvrajm, Sep 4, 2009 IP