smproph Posted January 21, 2011 Share Posted January 21, 2011 Ok I have a form where on of the inputs looks like this <input placeholder="Name" type="text" name="participant_name[]"/> Have to use that as array because I have a while loop that creates that input according to how many players they choose to be on their team. I need to be able to insert every participant_name into the participant field of my table. I was reading that maybe I need to do Implode or Explode, I'm not quite sure. If you want to see the form in action or look at it url is : http://snu.uintramural.com/form.php?mode=team Here's my php. $sql2="INSERT INTO teams (team_no,team_password) VALUES ( '$_POST[team]','$_POST[pw]')"; $result2=mysql_query($sql2) or die(mysql_error()); $sql="INSERT INTO participants (participant_name, team_no) VALUES ('$_POST[participant_name]','$_POST[team]')"; //echo $sql; $result=mysql_query($sql) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/225255-split-and-insert-array/ Share on other sites More sharing options...
kickstart Posted January 21, 2011 Share Posted January 21, 2011 Hi Should be possible using implode to put the array into a string. Use the rest of the values clause as the separator. $sql="INSERT INTO participants (participant_name, team_no) VALUES ('".implode("'$_POST[team]'),('",$_POST['participant_name'])."','$_POST[team]')"; All the best Keith Link to comment https://forums.phpfreaks.com/topic/225255-split-and-insert-array/#findComment-1163345 Share on other sites More sharing options...
smproph Posted January 22, 2011 Author Share Posted January 22, 2011 Hmm seem to be getting a problem. It spits out this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AAA'),('Eric'AAA'),('Joe','AAA')' at line 2 AAA is the Team name. It looks like to me maybe the " ' " are canceling out somewhere. ('Eric'AAA') has 3 " ' " while ('Joe','AAA') has 4 " ' ". And Ideas? Link to comment https://forums.phpfreaks.com/topic/225255-split-and-insert-array/#findComment-1163638 Share on other sites More sharing options...
kickstart Posted January 22, 2011 Share Posted January 22, 2011 Hi Suspect you are right. Just spotted it (missing comma as well):- $sql="INSERT INTO participants (participant_name, team_no) VALUES ('".implode("','$_POST[team]'),('",$_POST['participant_name'])."','$_POST[team]')"; If this doesn't fix it can you echo out and paste $sql? Sorry, on this laptop I have no way of testing it. All the best Keith Link to comment https://forums.phpfreaks.com/topic/225255-split-and-insert-array/#findComment-1163642 Share on other sites More sharing options...
smproph Posted January 22, 2011 Author Share Posted January 22, 2011 Worked perfect! Thank you Link to comment https://forums.phpfreaks.com/topic/225255-split-and-insert-array/#findComment-1163673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.