zid Posted May 12, 2014 Share Posted May 12, 2014 Hi, having a form that have unique name value so a submitted post would look like this if i run print_r($_POST): Array ( [19_y] => 1 [19_c] => this is a text message [18_y] => 1 [18_c] => this is another text message [17_n] => 1 [16_dn] => 1 [16_c] => [submit_questions] => ) Usually i have static values on the posts like $_POST['username']; But this is a survey so one can create a bunch of questions and each object like 19_y is uniqe. How can I bind this to something that I can post into mysql? Link to comment https://forums.phpfreaks.com/topic/288430-how-to-work-with-this-array/ Share on other sites More sharing options...
QuickOldCar Posted May 12, 2014 Share Posted May 12, 2014 I commented out the escaping so can run this code. $data = Array ("19_y" => "1", "19_c" => "this is a text message", "18_y" => "1", "18_c" => "this is another text message", "17_n" => "1", "16_dn" => "1", "16_c" => "submit_questions"); $columns = implode(", ",array_keys($data)); $escaped_values = array_map('trim', array_values($data)); //$escaped_values = array_map('mysqli_real_escape_string', array_values($escaped_values)); $values = implode(", ", $escaped_values); $sql = "INSERT INTO `tablename`($columns) VALUES ($values)"; echo $sql; results: INSERT INTO `tablename`(19_y, 19_c, 18_y, 18_c, 17_n, 16_dn, 16_c) VALUES (1, this is a text message, 1, this is another text message, 1, 1, submit_questions) Link to comment https://forums.phpfreaks.com/topic/288430-how-to-work-with-this-array/#findComment-1479169 Share on other sites More sharing options...
zid Posted May 12, 2014 Author Share Posted May 12, 2014 Hi, aiight, gonna give it a go and see if I can understand the code and if it works the way im trying to describe. havent used implode or array_map before... Link to comment https://forums.phpfreaks.com/topic/288430-how-to-work-with-this-array/#findComment-1479170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.