Welcome Guest, Not a member yet? Register   Sign In
Help to avoid sql injection attack
#1

[eluser]Unknown[/eluser]
would you please guide me to secure my query



here is the query
Quote:$id=$_GET['id'];
$sql="select title,picture,news from sport where id='$id'";
$result=mysql_query($sql,$db);



do u think its secure now


Quote:$id = mysql_real_escape_string($_GET['id']);
#2

[eluser]Bart v B[/eluser]
That can be much simpler Smile
UseActive Records

Code:
function GetData()
{
    $this->db->select('select title,picture,news');
    $this->db->where('id', $_GET['id']);
    $this->db->from('sport');
    
    $query = $this->db->get();
    
    foreach ($q->result() as $row)
    {
        $aData[] = $row;
        
    }
    
    return $aData;
}
#3

[eluser]CodeIgniteMe[/eluser]
+1 vote for Bart v B's answer.

only trimmed some redundant codes:
Code:
function GetData()
{
    $this->db->select('select title,picture,news');
    $this->db->where('id', $_GET['id']);
    
    $query = $this->db->get('sport');
    
    return $q->result();
}
#4

[eluser]Bart v B[/eluser]
[quote author="CodeIgniteMe" date="1312784824"]+1 vote for Bart v B's answer.

only trimmed some redundant codes:
Code:
function GetData()
{
    $this->db->select('select title,picture,news');
    $this->db->where('id', $_GET['id']);
    
    $query = $this->db->get('sport');
    
    return $q->result();
}
[/quote]

Pssst... Where is $q comming from? Smile

Code:
function GetData()
{
    $this->db->select('select title,picture,news');
    $this->db->where('id', $_GET['id']);
    
    $query = $this->db->get('sport');
    
    return $query->result();
}
#5

[eluser]CodeIgniteMe[/eluser]
haha sorry, I didn't see that. I only used your code as a reference :coolsmirk:
#6

[eluser]CodeIgniteMe[/eluser]
[quote author="Bart v B" date="1312855066"]
Code:
function GetData()
{
    $this->db->select('select title,picture,news');
    $this->db->where('id', $_GET['id']);
    
    $query = $this->db->get('sport');
    
    return $query->result();
}
[/quote]

and one more thing to clean up.
You don't need to include the select keyword in the statement
Code:
$this->db->select('select title,picture,news');
should be
Code:
$this->db->select('title,picture,news');
#7

[eluser]CodeIgniteMe[/eluser]
Or to make it all so short
Code:
function get_data()
{
    return $this->db->select('title,picture,news')->where('id', $_GET['id'])->get('sport')->result();
}

only works with PHP >= 5.0
Method Chaining
#8

[eluser]P.T.[/eluser]
Code:
function get_data()
{
    return $this->db->select('title,picture,news')->where('id', $this->input->get('id'))->get('sport')->result();
}




Theme © iAndrew 2016 - Forum software by © MyBB