htmlspecialchars

Discussion in 'PHP' started by roice, Oct 26, 2010.

  1. #1
    If I have this code:
    $query = mysql_query("SELECT`choice`,`name`  FROM `survey`  ");
    while($index = mysql_fetch_array($query))
    {
    $choice= $index['choice'];
    $name= $index['name'];
    echo $choice;
    echo $name;
    }
    PHP:
    Than I need to add "htmlspecialchars" here:
    $choice= htmlspecialchars($index['choice']);
    $name= htmlspecialchars($index['name']);
    echo $choice;
    echo $name;

    (variable "choise" was add into the DB by some user with some form)


    Can I just do htmlspecialchars($index) and than - $choice= $index['choice'];
    ?
     
    roice, Oct 26, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    no, that's not possible
     
    Rainulf, Oct 26, 2010 IP
  3. _:codefan:_

    _:codefan:_ Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #3
    No. You can do this:
    $index = array_map('htmlspecialchars', $index);
    PHP:
     
    _:codefan:_, Oct 26, 2010 IP
  4. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4

    If I don't have variable "$index", it won't do anything, right? so I can put this code in the top of every page...?
     
    roice, Oct 26, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    $index = array_map('htmlspecialchars', $index);
    PHP:
    That would apply the htmlspecialchars function to every value within the $index array...place that line under the while($index...)..
     
    danx10, Oct 26, 2010 IP
  6. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    well, In my website $index is the variable (array) that always hold the data for print to screen (echo), so I think its OK to always have
    $index = array_map('htmlspecialchars', $index);
    PHP:
    at the top of every page.
    If in specific page there is no printing to the screen so there will be no variable $index, and the code
    $index = array_map('htmlspecialchars', $index);
    PHP:
    won't do anything...
     
    roice, Oct 26, 2010 IP
  7. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Emm, That was q question...

    ?
     
    roice, Oct 27, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    If you just want $choice and $name to be converted into entities...do the following:

    $query = mysql_query("SELECT `choice`, `name` FROM `survey`");
    
    while($index = mysql_fetch_array($query)) {
    
    $choice = htmlspecialchars($index['choice']);
    
    $name = htmlspecialchars($index['name']);
    
    echo $choice;
    
    echo $name;
    
    }
    PHP:
    or if you want the whole array to be do the following:

    $query = mysql_query("SELECT `choice`, `name` FROM `survey`");
    
    while($index = mysql_fetch_array($query)) {
    
    $index = array_map('htmlspecialchars', $index);
    
    $choice= $index['choice'];
    
    $name= $index['name'];
    
    echo $choice;
    
    echo $name;
    
    }
    PHP:
     
    danx10, Oct 27, 2010 IP
  9. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    One more thing - when I'm using "mysql_real_escape_string" to escapre data before insert it into the DB, the code replace the nore ' to \'
    well, it's problem when my users write words like: Mike's phone ...
    What can I do to avoid that?
     
    roice, Oct 27, 2010 IP
  10. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    emm, someone?
     
    roice, Oct 30, 2010 IP
  11. KingOle

    KingOle Peon

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Use stripslashes() function when echoing out data?
     
    KingOle, Oct 30, 2010 IP
  12. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    stripslashes won't change '\ to '

    What else can I do?
     
    roice, Oct 31, 2010 IP
  13. KingOle

    KingOle Peon

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    It should do; it has to be used when you get the data from the database and output.

    You could try str_replace(); also which can replace any character in a string.
     
    KingOle, Oct 31, 2010 IP