Jump to content

Splitting up a string from a MySQL database to make a html form menu


Recommended Posts

Okay.. this might be simpler done than I know of, but I'm gonna give a shot at it myself though.

 

I have a database containing a table with information about members, for example one column could be favourite music artists and here I have information stored in this way: Kiss, Oasis, Etc, Etc.

While another member might only have one favourite artist: Sting

 

Now I need to build a menu list of all the favourite artists (with the use of the distinct method so that I don't get the same artist twice)

 

In some way I need to split up a string with more than one artist so that the menu won't list all these artists in one line.

All that I can think of is to integrate some kind of detection for the commas in the tables, but I'm not sure how to turn it into any useful code, any help would be greatly appreciated.

 

Alright... then the problem is the way I chose to store the names in the first place..

I did that by having them choose from a list of artists and then I used the following code before storing to Mysql:

 

$artists = implode(", ", $_POST[Artists]);

 

Any tips on how to store each with a seperate row?

Something like....

 

$artists = $_POST['Artists'];
foreach ($artists as $artist) {
  $artist = mysql_real_escape_string($artist);
  $id = $_SESSION['id'];
  $sql = "INSERT INTO tbl (artist_id, user_id) VALUES ($artist, $id)";
  mysql_query($sql);
}

Ok..

 

Here is my code:

 

 

if (!empty($_POST[Artists])) {

$artists = $_POST[Artists];

foreach ($artists as $artists)

  $artists = mysql_real_escape_string($artists);

} else {

 

die();

}

 

And then later I have my query with $artists to the database

No matter how many artists I select it will only add the last one I select.

 

 

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.