jesushax Posted July 30, 2008 Share Posted July 30, 2008 so this is how it works for updating a table $TSQL = "UPDATE tblDirectory SET"; foreach($_POST as $key => $value){ $TSQL .= " $key='".mysql_real_escape_string($value)."', "; } how would we do it for adding? no idea where to start here or am i going to ahve to write out each variable? Cheers Link to comment https://forums.phpfreaks.com/topic/117320-solved-loop-form-values-to-add-to-datbase/ Share on other sites More sharing options...
ronnie88 Posted July 30, 2008 Share Posted July 30, 2008 INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....) i.e. mysql_query("insert into projects(projects, username) values('$name', '$email')"); Link to comment https://forums.phpfreaks.com/topic/117320-solved-loop-form-values-to-add-to-datbase/#findComment-603442 Share on other sites More sharing options...
trq Posted July 30, 2008 Share Posted July 30, 2008 You really dont want to be doing this because your not sanatising the values prior to placing them in the db. $sql = "INSERT INTO tblDirectory ('" . implode("','",$_POST) . "')"; Link to comment https://forums.phpfreaks.com/topic/117320-solved-loop-form-values-to-add-to-datbase/#findComment-603443 Share on other sites More sharing options...
jesushax Posted July 30, 2008 Author Share Posted July 30, 2008 can i not add a mysql_real_escape_string to that aswell? Link to comment https://forums.phpfreaks.com/topic/117320-solved-loop-form-values-to-add-to-datbase/#findComment-603444 Share on other sites More sharing options...
trq Posted July 30, 2008 Share Posted July 30, 2008 Nope because it will run over the entire imploded string, not each element. You could use.... $a = array_map('mysql_real_escape_string',$_POST); $sql = "INSERT INTO tblDirectory ('" . implode("','",$a) . "')"; I suppose. Link to comment https://forums.phpfreaks.com/topic/117320-solved-loop-form-values-to-add-to-datbase/#findComment-603446 Share on other sites More sharing options...
jesushax Posted July 30, 2008 Author Share Posted July 30, 2008 what does imploding do? Link to comment https://forums.phpfreaks.com/topic/117320-solved-loop-form-values-to-add-to-datbase/#findComment-603449 Share on other sites More sharing options...
trq Posted July 30, 2008 Share Posted July 30, 2008 Take a look at the manual. Link to comment https://forums.phpfreaks.com/topic/117320-solved-loop-form-values-to-add-to-datbase/#findComment-603455 Share on other sites More sharing options...
jesushax Posted July 30, 2008 Author Share Posted July 30, 2008 thanks Link to comment https://forums.phpfreaks.com/topic/117320-solved-loop-form-values-to-add-to-datbase/#findComment-603459 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.