moagrius Posted November 30, 2008 Share Posted November 30, 2008 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 More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 1. mysql_real_escape_string(); 2. htmlspecialchars() followed by nl2br(). The order is important. Link to comment https://forums.phpfreaks.com/topic/134809-filtering-special-characters/#findComment-702012 Share on other sites More sharing options...
moagrius Posted November 30, 2008 Author Share Posted November 30, 2008 thanks for the quick reply - but after a quick lookup of nl2br it apparently swaps line breaks for "<br>" entities - i'm echoing javascript variables, not html - should i replace that step with a preg_replace? thanks again Link to comment https://forums.phpfreaks.com/topic/134809-filtering-special-characters/#findComment-702017 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.