Jump to content

Filtering Special Characters


moagrius

Recommended Posts

Hi,

 

I've got a form with several inputs and textareas.  Entries are saved to a SQL table, then later echo out a JSON object.

 

My question is 2 part:

1.  Since the user input will have to be passed to a SQL query, how should I pass the post variables to that string?  Poking around has led me to addslashes and sprintf, but experimentation hasn't produced predictable results.

2.  When echoing those entries, what formatting functions should be used to avoid conflicts with special characters (quotes and line breaks in particular, but I'd be interested if there were prefab functions available that I'm guessing exist but I'm not aware of...)?

 

E.G. on the former:

$querystring = array("");
foreach($_POST as $key=>$val) array_push($querystring,$val);
$querystring = implode("\",\"",$querystring);
$querystring = "INSERT INTO Topic VALUES (\"" . $querystring . "\")";
$result = mysql_query($querystring);
// What formatting functions should be applied to $val?

 

E.G. on the latter:

$result = mysql_query("select * from Topic");
$json = array();
while($row = mysql_fetch_assoc($result)){
$obj = array();
foreach($row as $key=>$val) array_push($obj,"\"" . $key . "\":\"" . $val . "\"");
$str = "{" . implode(",",$obj) . "}";
array_push($json,$str);
};
$json = implode(",",$json);
echo "var DB = [" . $json . "];\r\n\r\n";
// what formatting functions would be best applied to $key and $val?

 

Thanks in advance :)

Link to comment
https://forums.phpfreaks.com/topic/134809-filtering-special-characters/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.