pioneerx01 Posted July 21, 2011 Share Posted July 21, 2011 I am making a form and I am trying to figure out a proper way of trimming the user submitted data before I insert it into database. If I use such code: mysql_query("INSERT INTO ``.`` ( `ID` , `name` ) VALUES ( NULL , '$_POST[name]' );") or die(mysql_error()); I would do it this way but I am not sure if it is the best method: $name = trim($_POST[name]); mysql_query("INSERT INTO ``.`` ( `ID` , `name` ) VALUES ( NULL , '$name' );") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/242585-what-is-a-proper-way-to-trim-data-before-submitting-it-to-database/ Share on other sites More sharing options...
teynon Posted July 21, 2011 Share Posted July 21, 2011 That would trim the data. That's what trim does. You don't happen to mean validate do you? If you do mean trimming, then that is how you would do it. Quote Link to comment https://forums.phpfreaks.com/topic/242585-what-is-a-proper-way-to-trim-data-before-submitting-it-to-database/#findComment-1245927 Share on other sites More sharing options...
pioneerx01 Posted July 21, 2011 Author Share Posted July 21, 2011 Yes, just trim. I already have validation code in place. Quote Link to comment https://forums.phpfreaks.com/topic/242585-what-is-a-proper-way-to-trim-data-before-submitting-it-to-database/#findComment-1245929 Share on other sites More sharing options...
xyph Posted July 21, 2011 Share Posted July 21, 2011 There's no need to give it it's own variable, but you have the right idea. The only thing you're doing is not quoting associative array keys. This $_POST['name'] = trim($_POST['name']); mysql_query("INSERT INTO ``.`` ( `ID` , `name` ) VALUES ( NULL , '{$_POST['name']}' );") or die(mysql_error()); is pretty much the same as mysql_query("INSERT INTO ``.`` ( `ID` , `name` ) VALUES ( NULL , '". trim($_POST['name']) ."' );") or die(mysql_error()); I'd pick whichever one was easier to read and follow. The less you get lost looking back at your code, the better Quote Link to comment https://forums.phpfreaks.com/topic/242585-what-is-a-proper-way-to-trim-data-before-submitting-it-to-database/#findComment-1245930 Share on other sites More sharing options...
pioneerx01 Posted July 21, 2011 Author Share Posted July 21, 2011 perfect xyph, thanks. Less code is easier to manage and faster Quote Link to comment https://forums.phpfreaks.com/topic/242585-what-is-a-proper-way-to-trim-data-before-submitting-it-to-database/#findComment-1245937 Share on other sites More sharing options...
xyph Posted July 21, 2011 Share Posted July 21, 2011 Not to mention, redundant variables means memory allocated that doesn't need to be Glad I could help. Quote Link to comment https://forums.phpfreaks.com/topic/242585-what-is-a-proper-way-to-trim-data-before-submitting-it-to-database/#findComment-1245939 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.