Search code examples
phpescapingquotes

Escaping quotes in PHP with variable


There are numerous question on Stackoverflow concerning this but I have not been able to solve this still.

I'm trying to put multiple variables inside global attribute data-*. The output should be:

data-info="23;thisWeek"

The 23 comes from a database: $row["id"], and thisWeek from a variable: $categori1.

I've tried:

echo "<tr data-info=" .fixSlashes($row["id"], $categori1); " class=\"tableclass\"><td>"

Then

<?php
function fixSlashes($idP, $categoriP){
$str = addslashes($idP . ";" .$categoriP);
return $str;
}
?>

But something goes wrong. No error message, but the behaviour is wrong. It works if I just write:

echo "<tr data-info="data-info="23;thisWeek" class=\"tableclass\"><td>"

Solution

  • It looks like you are not outputting the html correctly. Note the escaped quotes around the fix Slashes function. Also note the "." instead of the semicolon.

    "<tr data-info=\"" .fixSlashes($row["id"], $categori1) . "\" class=\"tableclass\"><td>"
    

    If you want to see all errors include the following code. This would have caught the semicolon error.

    error_reporting(E_ALL);
    ini_set('display_errors', '1');