bcart Posted November 12, 2009 Share Posted November 12, 2009 Hi there Can someone tell me why this isn't working? $courses = array(); while($row_date = mysqli_fetch_array($r)) { $courses[$row_date['ctID']][] = $row_date['ctID']; } foreach ($test as $key => $value) { $insert_values .= "('{$_POST['course_date$key']}', '$id'), "; } $q_insert = "INSERT INTO induction_attendees (course, attendee) VALUES ".substr($insert_values, 0, -2).""; When I output the query it reads as follows INSERT INTO induction_attendees (course, attendee) VALUES ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51'), ('', '51') It's not picking up the $_POST['course_date$key'] part. Can anyone help? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/181236-dynamic-array/ Share on other sites More sharing options...
trq Posted November 12, 2009 Share Posted November 12, 2009 Variables are not interpolated within single quoted strings. Quote Link to comment https://forums.phpfreaks.com/topic/181236-dynamic-array/#findComment-956100 Share on other sites More sharing options...
bcart Posted November 12, 2009 Author Share Posted November 12, 2009 Variables are not interpolated within single quoted strings. I'm sorry I don't quite understand. What should it be? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/181236-dynamic-array/#findComment-956128 Share on other sites More sharing options...
sasa Posted November 12, 2009 Share Posted November 12, 2009 try $insert_values .= "('". $_POST['course_date'.$key]."', '$id'), "; Quote Link to comment https://forums.phpfreaks.com/topic/181236-dynamic-array/#findComment-956196 Share on other sites More sharing options...
Mchl Posted November 12, 2009 Share Posted November 12, 2009 And add some escaping to that (mysql_real_escape_string probably) Quote Link to comment https://forums.phpfreaks.com/topic/181236-dynamic-array/#findComment-956199 Share on other sites More sharing options...
JustLikeIcarus Posted November 12, 2009 Share Posted November 12, 2009 If you put $var inside of single quites like '$var' then that literally mean '$var' it doesnt interpolate $var. One way you could do this would be: foreach ($test as $key => $value) { $key = 'course_date'.$key; $insert_values .= "(\'".$_POST[$key]."\', \'$id\'), "; } That should work but you should probably look into using bind variables where appropriate. Quote Link to comment https://forums.phpfreaks.com/topic/181236-dynamic-array/#findComment-956200 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.