Jump to content

Split and INSERT array


smproph

Recommended Posts

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

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

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.