kevinkorb Posted December 15, 2006 Share Posted December 15, 2006 Is this effective in preventing SQL injections.. it seems like it would be to me. What would make this not generally ideal to do before entering user submitted data into the database?[code]<?phpClass Filter { public static function clean_array($array) { foreach($array AS $key => $val) { $array[$key] = mysqli_real_escape_string($val); } }}// Then cleaning the post array.$clean = Filter::clean_array($_POST);$query = "INSERT INTO table (f1,f2,f3) VALUES ('{$clean['f1']'}, '{$clean['f2']'}, '{$clean['f3']'});";mysqli_query($db, $query);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30727-sql-injection-is-this-effective/ Share on other sites More sharing options...
Daniel0 Posted December 15, 2006 Share Posted December 15, 2006 Looks fine to me.Consider this though:[code]<?php$clean = array_map('mysql_real_escape_string',$_POST);$query = "INSERT INTO table (f1,f2,f3) VALUES ('{$clean['f1']}', '{$clean['f2']}', '{$clean['f3']}');";mysqli_query($db, $query);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30727-sql-injection-is-this-effective/#findComment-141612 Share on other sites More sharing options...
kevinkorb Posted December 15, 2006 Author Share Posted December 15, 2006 Ya, but the good thing about the other one is you can litter that all throughout your project. If you find a better escaping method you can just change the 'clean_array()' method once to use whatever logic you'd use to clean all your arrays.AbstractionBut I you made a good point with the array_map function. So change the class to be[code]<?phpClass Filter { public static function clean_array($array) { return array_map('mysql_real_escape_string', $array); }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30727-sql-injection-is-this-effective/#findComment-141617 Share on other sites More sharing options...
Daniel0 Posted December 15, 2006 Share Posted December 15, 2006 Yeah, well... that was my point. I supposed you were able to figure it out yourself (which you did). Quote Link to comment https://forums.phpfreaks.com/topic/30727-sql-injection-is-this-effective/#findComment-141712 Share on other sites More sharing options...
Eric_Ryk Posted December 15, 2006 Share Posted December 15, 2006 Though that does work it is probably also a good idea to make sure that you are only getting the characters that you are supposed to using regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/30727-sql-injection-is-this-effective/#findComment-141821 Share on other sites More sharing options...
Daniel0 Posted December 17, 2006 Share Posted December 17, 2006 mysql_real_escape_string() gets all the characters that causes a security risk. An important thing to note is that the characters % and _ which are considered as wildcards if combined with GRANT, LIKE and REVOKE will not be escaped so if you are going to put a string into a such query you must manually escape those characters as well. Quote Link to comment https://forums.phpfreaks.com/topic/30727-sql-injection-is-this-effective/#findComment-143005 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.