Seperate textarea field and post to DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wescrock
Forum Commoner
Posts: 31
Joined: Wed Sep 10, 2008 10:31 am
Location: Fresno, CA

Seperate textarea field and post to DB

Post by wescrock »

Hello all,
I have a text area field that will handle citation keywords (much like blog tags) where each keyword will be separated by a line break (\n.) My question is, on the execute page, how would I go about separating the text area data and posting it to the database. I am sure it will take a while loop...

Here is my form code:

Code: Select all

 
  <label for="keywords">Keywords:</label>
  <Textarea name="keywords" cols="30" rows="2"></textarea><br />
 
Here is the PHP on the post page:

Code: Select all

 
    $sql="INSERT INTO wfbiblio.keywords (keyword) VALUES ('$keywords')";
    $result = mysql_query($sql) or die(mysql_error());
    $new_keyword_id = mysql_insert_id(); //retrieves publisher_id_
 
    $sql="INSERT INTO wfbiblio.keyword_link (keyword_id, citation_id) VALUES ('$new_keyword_id','$new_citation_id')";
    $result = mysql_query($sql) or die(mysql_error());
 
thanks for any help you can give!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Seperate textarea field and post to DB

Post by aceconcepts »

You could try:

Code: Select all

 
$arr_keywords= explode("\n", trim($keywords));
 
wescrock
Forum Commoner
Posts: 31
Joined: Wed Sep 10, 2008 10:31 am
Location: Fresno, CA

Re: Seperate textarea field and post to DB

Post by wescrock »

Ok. so that will separate the keywords correct? how do i post them to the database, each as a separate entry into the db.'

thanks, and sorry for my lack of knowledge... I know how these things work... just not how to execute it... lets just say... They like to throw me into projects that far exceed my experience ;)
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Seperate textarea field and post to DB

Post by aceconcepts »

This is one way you could do it:

Code: Select all

 
//CREATE ARRAY OF ALL KEYWORDS
$arr_keywords= explode("\n", trim($keywords));
 
//LOOP THROUGH ALL KEYWORDS
foreach($arr_keywords as $value)
{
   if(!empty($value))//MAKE SURE ITS NOT EMPTY
   {
      //INSERT INTO DB
   }
}
 
wescrock
Forum Commoner
Posts: 31
Joined: Wed Sep 10, 2008 10:31 am
Location: Fresno, CA

Re: Seperate textarea field and post to DB

Post by wescrock »

OOOH! so $arr creates an array? that's pretty freakin sweet.

man, thanks for your help, I'm going to try this right now.
wescrock
Forum Commoner
Posts: 31
Joined: Wed Sep 10, 2008 10:31 am
Location: Fresno, CA

Re: Seperate textarea field and post to DB

Post by wescrock »

O.k. So, i posted sample data, and it all went in WHERE it should (both the keywords table as well as the keyword_link table)... but in the keywords table, the data just shows the word 'array'

should i be posting $keywords or $arr_keywords? (i did $arr_keywords)

thanks,
Wes

EDIT: I changed it to $value, which then posted the CORRECT data into the database, but it keeps has the paragraph markup in the data, is there a way to parse that out of there?
Last edited by wescrock on Mon Oct 13, 2008 10:41 am, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Seperate textarea field and post to DB

Post by aceconcepts »

Within the foreach() loop you should be INSERTING $value as the keyword.
wescrock
Forum Commoner
Posts: 31
Joined: Wed Sep 10, 2008 10:31 am
Location: Fresno, CA

Re: Seperate textarea field and post to DB

Post by wescrock »

I changed it to $value, which then posted the CORRECT data into the database, but it keeps has the paragraph markup in the data, is there a way to parse that out of there?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Seperate textarea field and post to DB

Post by aceconcepts »

wescrock
Forum Commoner
Posts: 31
Joined: Wed Sep 10, 2008 10:31 am
Location: Fresno, CA

Re: Seperate textarea field and post to DB

Post by wescrock »

Great. Got it working! thank you so much for your help.
Post Reply