Jump to content

[SOLVED] explode() with database insertion


Recommended Posts

I am just double checking, can i use the explode() function to help insert information into my database

 

i have a field that is set to field of study where they can enter more than one field separated by a comma (,). Is there a way that I can use the explode() function to get each one in a separate line in the database?

 

For example they enter:

 

Network Technology, Computer Technical Support, Network Specialist

 

In the database table that I am saving all the information there is a column called Field of Study I would like it to be displayed this way:

 

Network Technology

Computer Technical Support

Network Specialist

 

not this way

 

Network Technology, Computer Technical Support, Network Specialist

 

Thanks

 

Mbrown

Link to comment
https://forums.phpfreaks.com/topic/124645-solved-explode-with-database-insertion/
Share on other sites

Yes, after you explode the string with the delimiter of ',' it will divide up all three of those fields into pieces.  So:

 

$pieces[0] = Network Technology;

$pieces[1] = Computer Technical Support;

$pieces[2] = Network Specialist;

 

Now you can insert however you want, even by adding line breaks in.

This is in my validate.php

....
			//replace the ", " with a new line character in the database 
			//table column for field of study
			$FieldofStudyArray = explode (", ", $FieldofStudy);
			foreach ($FieldofStudyArray as $FieldsofStudy)
....

 

 

this is in my insert.php

....
<body>
<?php

$query = "INSERT INTO survey (Name, Age, Month, Day, Year, Hometown, State, Graduation, Certificate, Associates, Bachelors, Study, GPA) VALUES('$Name', '$Age', '$Month', '$DayofBirth','$birthYear', '$hometown', '$State', '$YearofGrad', '$Certificate', '$Associates', '$Bachelors', '$FieldOfStudy<br />', '$GPA');";

mysqli_query($db, $query) or die(mysqli_error($db) . '<br />' . $query);?>
....

 

I can show you what i have in my getdata.php which just shows the information in my database so i dont need to go into the phpmyadmin area for quickness sake.

 

any help would be great

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.