Jump to content

Add a comma delimited string to a database


sean04

Recommended Posts

As of now I have a textbox that I will tell users to place a comma in between each word they write. I would like all of these words to be broken up by the commas and inserted into a database. The database is something like this and the text field like this

 

User 1

Enter Activities: Hockey,Soccer,Running

User 2

Enter Activities: Baseball

 

Activities Database:

 

User_ID      Activity

1                Hockey

1                Soccer

1                Running

2                Baseball

 

 

Any code samples or links will be great! Thanks!

Link to comment
Share on other sites

You really don't want to store them like that; that breaks database normalization. Create another table and insert each activity as its own record using the user_id as a foreign key for each record. On the form, you can either let them have multiple text inputs, or better still, define an array of check boxes for them, and let them select up to whatever maximum number of checkboxes you want.

Link to comment
Share on other sites

Thank you Pikachu2000. That sounds like a reasonable way to do it. I will take a look into that. That being said, I'm still interested on how I would come about completing this in a way that my first post stated.

 

If I have something like this:

 

<?PHP

$InterestsList = $_POST[interests];
$Interests= explode(",", $InterestsList);

?>

 

After something like that, how would I loop all the values into a table that looks like the one in my first post?

 

Thanks for the help!

 

Link to comment
Share on other sites

Psuedo code...

 

$InterestsList = $_POST[interests];
$Interests= explode(",", $InterestsList);
count the number of elements in the $interests array
connect to database
$i=0;
while ($1 < count)
$variable = array value[$l]
  insert into table field $variable
}
?>

 

make sense?

Link to comment
Share on other sites

So something like this? I haven't tried it yet :P

 

<?PHP
$InterestsList = $_POST[interests];
$Interests= explode(",", $InterestsList);
$InterestsCount = count($Interests);
connect to database
$l=0;
while ($1 < InterestsCount)
$variable = array value[$l]
  $result = mysql_query(insert into interests (User_ID,Interests) values($loggedin[user_ID],$variable));
}
?>

 

Link to comment
Share on other sites

<?php
    
$_POST['activities'] = 'foo,bar,    ,world'; // TODO Remove this line

$user_id = 1; // Assume you have this elsewhere in your code or can set it here
$activities = array_key_exists( 'activities', $_POST ) ? explode( ',', $_POST['activities'] ) : false;
if( $activities ) {
    foreach( $activities as $k => $v ) {
        // We want to clean up each activity
        $v = trim( $v );
        if( ! strlen( $v ) ) {
            // Remove empty activities
            unset( $activities[ $k ] );
        }else{
            //$acitivities[ $k ] = mysql_real_escape_string( $v ); // TODO Uncomment this line
        }
    }
    
    // $activities array is now clean and void of empty values, but if the array
    // itself is empty we do nothing
    if( count( $activities ) > 0 ) {
        $sql = "insert into `my_table` ( `user_id`, `activity` ) values" . PHP_EOL;
        $sql .= "( {$user_id}, '" . implode( "' )" . PHP_EOL . "( {$user_id}, '", $activities ) . "' )" . PHP_EOL;
        echo $sql; // TODO Execute query instead of echo
    }else{
        echo 'Nothing to insert.'; // TODO Print better message?
    }
}else{
    echo 'Activities not in POST.';  // TODO Print better message?
}
?>

Link to comment
Share on other sites

what I wrote was NOT the exact coding (hence PSUEDO code)

 

The purpose of $i is simply to keep looping thru the array until all the elements have been processed. Perhaps some extra commenting may help

 

<?PHP

$InterestsList = $_POST[interests]; // get the variable from the form

$Interests= explode(",", $InterestsList); // creates an array where each word that was separated by comma becomes an element in the array

$InterestsCount = count($Interests); // Counts the total elements in the array

connect to database // Do you actual database connection here

$i=0; // set your counter

while ($i < $InterestsCount) { // begin your loop

  $what_interest = $Interests[$i]; // get the value of the current element

  $result = mysql_query("insert into interests (User_ID,Interests) values('$loggedin[user_ID]', '$what_interest')"; // insert appropriate values into the database

  $i = $ i + 1; // increment your counter
}
?>

 

any clearer?

 

 

Link to comment
Share on other sites

Hey thanks for the help! I managed to complete that.

 

I have another question now though. How would I add checkbox values to a table. Like I have 4 checkboxes and the user can select as many as they want. how would I get all those that they checked and add them to my table? The table I guess would look like the one that I showed on my first post

 

Thanks

Link to comment
Share on other sites

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.