Text areas (5), loop through, collect, insert .. ??

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
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Text areas (5), loop through, collect, insert .. ??

Post by MiniMonty »

Hi all,

though I'd ask for some advice before I start !

I have a script which uploads 5 images to a dir, and inserts the image path into an SQL db.
Taken me a while to learn how to do this with a multiple form and a foreach() loop but I got there :)
So now I'd like to add the ability for users to comment on the images they are about to upload.
I've added text areas next to each filefield button and they are all named textarea[].

And then I'm stuck - never dealt with textfields before, never inserted text into a DB so where do I start ?
For instance, with the image upload script I use thi to get going:

Code: Select all

 
  for ($i=0;$i<5;$i++)
  {
if ((!empty($_FILES['userfile'])) && ($_FILES['userfile']['error'][$i] == 0)){
    $image = $_FILES['userfile']['name'][$i];
    $filename = stripslashes($_FILES['userfile']['name'][$i]);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
 
etc etc etc
 
So for instance what would the equivalent to $_FILES be for a text area ?
Grateful for any links, tips, tricks, pointers or samples !

Best wishes
Monty
Last edited by MiniMonty on Thu Feb 04, 2010 3:43 pm, edited 1 time in total.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Text areas (5), loop through, collect, insert .. ??

Post by klevis miho »

try this

$comments = $_POST['textarea'];
echo $comments;
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Text areas (5), loop through, collect, insert .. ??

Post by AbraCadaver »

If I remember the code from your last post, your $_FILES['userfile'][] and your $_POST['textarea'][] should match, so as you're looping through $_FILES['userfile']['name'][$i] corresponds to $_POST['textarea'][$i]. So just use $_POST['textarea'][$i] where you want it. Make sure to run it through mysql_real_escape_string() before inserting.

As an example:

Code: Select all

for ($i=0;$i<5;$i++)
  {
    // changed this to isset($_FILES['userfile']['name'][$i])
    if ((isset($_FILES['userfile']['name'][$i])) && ($_FILES['userfile']['error'][$i] == 0)){
        $image = $_FILES['userfile']['name'][$i];
        $filename = stripslashes($_FILES['userfile']['name'][$i]);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
 
        $comments = stripslashes($_POST['textarea'][$i]);
 
// etc etc etc
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Text areas (5), loop through, collect, insert .. ??

Post by MiniMonty »

Simple and elegant - should have seen that !
Thanks.

You know what it's like when you've been looking at something for too long and really
can't see the wood for the trees.... :roll:
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Text areas (5), loop through, collect, insert .. ??

Post by MiniMonty »

OK _ next step...

If you guys were going to hold his data (the path to the images and the comments that go with each one),
how would you deal with the text of the comments on the DB?

I'm thinking as one block of text in a varchar separated by &1= &2= but is there a better way ?
I'll need (eventually) to pull this out and display the users uploaded pictures and the comments that
go with each one.

Users can enter up to 2000 characters per picture so the comments of any one user on any set of five pictures could
be up to 10,000 characters in total. I have very limited experience with SQL - would a blob be a better option ?

Best wishes
Monty
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Text areas (5), loop through, collect, insert .. ??

Post by AbraCadaver »

MiniMonty wrote:OK _ next step...

If you guys were going to hold his data (the path to the images and the comments that go with each one),
how would you deal with the text of the comments on the DB?

I'm thinking as one block of text in a varchar separated by &1= &2= but is there a better way ?
I'll need (eventually) to pull this out and display the users uploaded pictures and the comments that
go with each one.

Users can enter up to 2000 characters per picture so the comments of any one user on any set of five pictures could
be up to 10,000 characters in total. I have very limited experience with SQL - would a blob be a better option ?

Best wishes
Monty
At a minimum have 3 fields in your database table. An auto increment INTEGER field `id` as the primary key, an `img_path` field of VARCHAR(whatever length you need), I would say 255 would be fine, and a `comment` field type TEXT.

Each image gets it's own row, so 1 insert per image.

Of course run each var through mysql_real_escape_string() before insert and later when you pull the comments out of the database and display them you'll probably want nl2br().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply