timmah1 Posted May 12, 2010 Share Posted May 12, 2010 I'm trying to get each value to insert into it's own row, I'm sure this is simple, but I can't figure it out. Right now, it inserts every value on the same line id username 10 test, test1, test2 I need it to insert everything separately id username 10 test 10 test1 10 test2 Here is my code $usernames = implode(", ", $username); for ($i = 0; $i < count($usernames); $i++) { mysql_query("INSERT INTO johns_username (id, username) VALUES('$UID','$usernames') ") or die(mysql_error()); } Can anybody help me out? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/201501-inserting-multiple-rows/ Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 implode creates a string from an array. Your code doesn't really make allot of sense. Link to comment https://forums.phpfreaks.com/topic/201501-inserting-multiple-rows/#findComment-1057146 Share on other sites More sharing options...
timmah1 Posted May 12, 2010 Author Share Posted May 12, 2010 ok, let me try again. I used implode to see if I was getting all the names, which it echoes every name that is sent properly I have a form which allows you to add more than one username <input name="username[]" type="text" id="username[]" size="50" /> I then want each username to be in its own row, right now, it is inserting all the usernames in the same row $count = count($_POST['username']); for ($i = 0; $i < $count; $i++) { $username = $_POST['username'][$i]; mysql_query("INSERT INTO johns_username (id, username) VALUES('$UID','$username') ") or die(mysql_error()); } How can I get each username into its own row? Link to comment https://forums.phpfreaks.com/topic/201501-inserting-multiple-rows/#findComment-1057177 Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 foreach ($_POST['username'] as $name) { mysql_query("INSERT INTO johns_username (id, username) VALUES ('$UID','$name')"); } Link to comment https://forums.phpfreaks.com/topic/201501-inserting-multiple-rows/#findComment-1057185 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.