Page 1 of 1

Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 9:40 am
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!

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 9:50 am
by aceconcepts
You could try:

Code: Select all

 
$arr_keywords= explode("\n", trim($keywords));
 

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 10:23 am
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 ;)

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 10:27 am
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
   }
}
 

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 10:29 am
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.

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 10:38 am
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?

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 10:41 am
by aceconcepts
Within the foreach() loop you should be INSERTING $value as the keyword.

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 10:45 am
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?

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 10:48 am
by aceconcepts

Re: Seperate textarea field and post to DB

Posted: Mon Oct 13, 2008 12:16 pm
by wescrock
Great. Got it working! thank you so much for your help.