[PHP-DB] Arrays from forms....

2006-11-30 Thread Gary E. Terry
I have a problem. I have done this before, but can't find the files. 

I am using the following function to upload files from a form.

foreach ($_FILES[pictures][error] as $key = $error) {
   if ($error == UPLOAD_ERR_OK) {
   echo $error_codes[$error];
   move_uploaded_file(
 $_FILES[pictures][tmp_name][$key],
 images/ .$_FILES[pictures][name][$key]
   ) or die(Problems with upload);
   }
}

I also need to insert the filenames of the uploaded files into a database.

There are three fields in my db for these filenames, image1 image2 and
image3. 

What would be the best way to get those filenames from the array into the
vars $image1 $image2 and $image3?

the first file in the array should be $image1 and so on.

Thanks in advance.




smime.p7s
Description: S/MIME cryptographic signature


[PHP-DB] Multiple inserts into a database

2006-11-30 Thread Daniel Smith
The kind of thing I'm looking to do is to insert a variable number of
records into a database as a consequence of a form entry.

I am using PHP with a MySQL database to store information that is
vaguely analogous to a fast food online ordering system. I have a table
containing individual items with a short code for each item.  A user
would enter the short code and this gets stored.

x---x
| item_id | item_name  | item_code  |
|1|   Burger   |  bur   |
|2|   Fries|  fr|
|3|   Cola |  co|
x---x

What I want to do is have a way of letting the customer order multiple
items with one code, e.g. entering meal results in burger/fries/cola
being entered in an order table.

I realise i need in effect a translation table, that contains meal with
references to the items burger/frires/cola, something like below so I
could do something SELECT item_id FROM deal WHERE deal_name = meal to
get the individual items for the meal

x---x
| deal_id | deal_name  | item_id|
|1|   meal |  1 |
|2|   meal |  2 |
|3|   meal |  3 |
x---x

So far, I can understand what I need to do to achieve this.  What I am
having trouble trying to understand what to do and how best to do it, is
to get the individual elements of the meal entered into an order table.

Would a foreach statement that works through the array produced by the
meal query, inserting each item in the meal be the best way forward?

The deals on offer would vary in size e.g. burger+fries/burger+fries
+cola+ice cream+toy so whatever solution is use, would have to cope with
changing size.

Note:  As you might have noticed already, I'm not the world's best
PHP/MySQL programmer but I'm ready and willing to try stuff.


Danny

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Arrays from forms....

2006-11-30 Thread Chris

Gary E. Terry wrote:
I have a problem. I have done this before, but can't find the files. 


I am using the following function to upload files from a form.

foreach ($_FILES[pictures][error] as $key = $error) {
   if ($error == UPLOAD_ERR_OK) {
   echo $error_codes[$error];
   move_uploaded_file(
 $_FILES[pictures][tmp_name][$key],
 images/ .$_FILES[pictures][name][$key]
   ) or die(Problems with upload);
   }
}

I also need to insert the filenames of the uploaded files into a database.

There are three fields in my db for these filenames, image1 image2 and
image3. 


What would be the best way to get those filenames from the array into the
vars $image1 $image2 and $image3?


You want variable variables:

http://www.php.net/manual/en/language.variables.variable.php

?php
for ($i = 1; $i  5; $i++) {
  ${image.$i} = i is  . $i;
}
echo $image4 . \n;
?

i is 4


so after the or die:

${image.$key} = $_FILES['pictures']['name'];

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Multiple inserts into a database

2006-11-30 Thread Chris

Daniel Smith wrote:

The kind of thing I'm looking to do is to insert a variable number of
records into a database as a consequence of a form entry.

I am using PHP with a MySQL database to store information that is
vaguely analogous to a fast food online ordering system. I have a table
containing individual items with a short code for each item.  A user
would enter the short code and this gets stored.

x---x
| item_id | item_name  | item_code  |
|1|   Burger   |  bur   |
|2|   Fries|  fr|
|3|   Cola |  co|
x---x

What I want to do is have a way of letting the customer order multiple
items with one code, e.g. entering meal results in burger/fries/cola
being entered in an order table.

I realise i need in effect a translation table, that contains meal with
references to the items burger/frires/cola, something like below so I
could do something SELECT item_id FROM deal WHERE deal_name = meal to
get the individual items for the meal

x---x
| deal_id | deal_name  | item_id|
|1|   meal |  1 |
|2|   meal |  2 |
|3|   meal |  3 |
x---x

So far, I can understand what I need to do to achieve this.  What I am
having trouble trying to understand what to do and how best to do it, is
to get the individual elements of the meal entered into an order table.

Would a foreach statement that works through the array produced by the
meal query, inserting each item in the meal be the best way forward?

The deals on offer would vary in size e.g. burger+fries/burger+fries
+cola+ice cream+toy so whatever solution is use, would have to cope with
changing size.

Note:  As you might have noticed already, I'm not the world's best
PHP/MySQL programmer but I'm ready and willing to try stuff.


I'd do it a little differently.

create table deals(dealid, dealname);

create table items(itemid, itemname);

create table deals_items(dealid, itemid);

Then you have something like this:

-
| dealid | dealname |
-
| 1  | meal 1   |
| 2  | meal 2   |
.

the items table contains
-
| itemid | itemname |
-
| 1  | burger 1 |
| 2  | burger 2 |
| 3  | chips|
| 4  | drink|
.

then for the deal_items table it joins both together:

---
| dealid | itemid |
---
| 1  | 1  |
| 1  | 3  |
| 1  | 4  |


To get everything for meal 1 you can then do a simple join query:

select itemname from deals d, items i, deal_items di where 
d.dealid=di.dealid and i.itemid = di.itemid and dealname=meal 1;


You'll need an index on deal_items(dealid, itemid) and dealid  itemid 
in their own tables will be primary keys.


This is just called a many-to-many relationship.

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Arrays from forms....

2006-11-30 Thread Chris

Gary E. Terry wrote:

I don't know if we are on the same page here... Maybe we are and I am just
an idiot.

The fields in my db are image1 image2 and image3.

There are only three fields in the form that is being passed to the
php script. But, on the form they are named pictures[].

Here is the full php I am using to process the form output:

?php
include db.inc.back.php;
foreach ($_FILES[pictures][error] as $key = $error) {
   if ($error == UPLOAD_ERR_OK) {
   echo $error_codes[$error];
   move_uploaded_file(
 $_FILES[pictures][tmp_name][$key],
 bikeimages/ .$_FILES[pictures][name][$key]
   ) or die(Problems with upload);
   ${image.$key} = $_FILES['pictures']['name'];
   }
}

$make = $_POST['make'];
$model = $_POST['model'];
$year = $_POST['year'];
$color = $_POST['color'];
$description = $_POST['description'];
$status = $_POST['status'];

$query = INSERT INTO `inventory` (`make`, `model`, `year`, `color`,
`description`, `image1`, `image2`, `image3`, `status`)
VALUES ('$make', '$model', '$year', '$color', '$description',
'$image1', '$image2', '$image3', '$status');;

$result = mysql_db_query($db, $query) or displayErrorQuery ($query);

echo meta http-equiv='refresh' content='0;URL=inventorylist.php';
?


$image1 $image2 and $image3 are not getting defined... Not real sure what's
going on.


Always cc the list so others can learn or suggest things.

Are you uploading 3 images? Maybe define them first as well:

foreach ($_FILES .. ) {
  ${image.$key} = ;

  if ($error == 


or do you mean the values are always empty?

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DB] Arrays from forms....

2006-11-30 Thread Gary E. Terry
Sorry about that, not CC'ing the list...

the values are not always empty, they should always have something in
them...  

There should be 3 images uploading. But, I am sure just to make life
interesting,
there will be times when there could be as little as one uploaded.

-Original Message-
From: Chris [mailto:[EMAIL PROTECTED] 
Sent: Thursday, November 30, 2006 10:10 PM
To: Gary E. Terry
Cc: PHP DB
Subject: Re: [PHP-DB] Arrays from forms

Gary E. Terry wrote:
 I don't know if we are on the same page here... Maybe we are and I am just
 an idiot.
 
 The fields in my db are image1 image2 and image3.
 
 There are only three fields in the form that is being passed to the
 php script. But, on the form they are named pictures[].
 
 Here is the full php I am using to process the form output:
 
 ?php
 include db.inc.back.php;
 foreach ($_FILES[pictures][error] as $key = $error) {
if ($error == UPLOAD_ERR_OK) {
echo $error_codes[$error];
move_uploaded_file(
  $_FILES[pictures][tmp_name][$key],
  bikeimages/ .$_FILES[pictures][name][$key]
) or die(Problems with upload);
${image.$key} = $_FILES['pictures']['name'];
}
 }
 
 $make = $_POST['make'];
 $model = $_POST['model'];
 $year = $_POST['year'];
 $color = $_POST['color'];
 $description = $_POST['description'];
 $status = $_POST['status'];
 
 $query = INSERT INTO `inventory` (`make`, `model`, `year`, `color`,
 `description`, `image1`, `image2`, `image3`, `status`)
 VALUES ('$make', '$model', '$year', '$color', '$description',
 '$image1', '$image2', '$image3', '$status');;
 
 $result = mysql_db_query($db, $query) or displayErrorQuery ($query);
 
 echo meta http-equiv='refresh' content='0;URL=inventorylist.php';
 ?
 
 
 $image1 $image2 and $image3 are not getting defined... Not real sure
what's
 going on.

Always cc the list so others can learn or suggest things.

Are you uploading 3 images? Maybe define them first as well:

foreach ($_FILES .. ) {
   ${image.$key} = ;

   if ($error == 


or do you mean the values are always empty?

-- 
Postgresql  php tutorials
http://www.designmagick.com/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



smime.p7s
Description: S/MIME cryptographic signature


Re: [PHP-DB] Arrays from forms....

2006-11-30 Thread Chris

Gary E. Terry wrote:

Sorry about that, not CC'ing the list...

the values are not always empty, they should always have something in
them...  


There should be 3 images uploading. But, I am sure just to make life
interesting,
there will be times when there could be as little as one uploaded.


If you print them out what's in them?

echo $image1;
echo $image2;

I just realized that the $key probably starts at 0 instead of 1, so you 
might need to check that and then possibly:


${image.($key+1)} = $_FILES['pictures']['name'];

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DB] Arrays from forms....

2006-11-30 Thread Gary E. Terry
If I print them, there's nothing. all three

Here is the same file, in test mode... 

?php
include db.inc.back.php;


foreach ($_FILES[pictures][error] as $key = $error) {
   if ($error == UPLOAD_ERR_OK) {
   echo $error_codes[$error];
   move_uploaded_file(
 $_FILES[pictures][tmp_name][$key],
 bikeimages/ .$_FILES[pictures][name][$key]
   ) or die(Problems with upload);
   ${image.$key} = $_FILES['pictures']['name'];
   }
}

$make = $_POST['make'];
$model = $_POST['model'];
$year = $_POST['year'];
$color = $_POST['color'];
$description = $_POST['description'];
//$image1 = $_POST['image1'];
//$image2 = $_POST['image2'];
//$image3 = $_POST['image3'];
$status = $_POST['status'];

echo $image1 -- $image2 -- $image3;
/*
$query = INSERT INTO `inventory` (`make`, `model`, `year`, `color`,
`description`, `image1`, `image2`, `image3`, `status`)
VALUES ('$make', '$model', '$year', '$color', '$description',
'$image1', '$image2', '$image3', '$status');;

$result = mysql_db_query($db, $query) or displayErrorQuery ($query);

echo meta http-equiv='refresh' content='0;URL=inventorylist.php';
exit;
*/
?

 I get  -- --  outputted.

-Original Message-
From: Chris [mailto:[EMAIL PROTECTED] 
Sent: Thursday, November 30, 2006 10:39 PM
To: Gary E. Terry
Cc: php-db@lists.php.net
Subject: Re: [PHP-DB] Arrays from forms

Gary E. Terry wrote:
 Sorry about that, not CC'ing the list...
 
 the values are not always empty, they should always have something in
 them...  
 
 There should be 3 images uploading. But, I am sure just to make life
 interesting,
 there will be times when there could be as little as one uploaded.

If you print them out what's in them?

echo $image1;
echo $image2;

I just realized that the $key probably starts at 0 instead of 1, so you 
might need to check that and then possibly:

${image.($key+1)} = $_FILES['pictures']['name'];

-- 
Postgresql  php tutorials
http://www.designmagick.com/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



smime.p7s
Description: S/MIME cryptographic signature


Re: [PHP-DB] Arrays from forms....

2006-11-30 Thread Chris

Gary E. Terry wrote:

If I print them, there's nothing. all three

Here is the same file, in test mode... 


?php
include db.inc.back.php;


foreach ($_FILES[pictures][error] as $key = $error) {
   if ($error == UPLOAD_ERR_OK) {
   echo $error_codes[$error];
   move_uploaded_file(
 $_FILES[pictures][tmp_name][$key],
 bikeimages/ .$_FILES[pictures][name][$key]
   ) or die(Problems with upload);
   ${image.$key} = $_FILES['pictures']['name'];
   }
}


Hmm, looks ok.

If you:

print_r($_FILES[pictures]);

and

print_r($_FILES[pictures]['error']);

do you get stuff there ?

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DB] Arrays from forms....

2006-11-30 Thread Gary E. Terry
Nope...  here is the output.

-- -- Array ( [error] = )

-Original Message-
From: Chris [mailto:[EMAIL PROTECTED] 
Sent: Thursday, November 30, 2006 10:56 PM
To: Gary E. Terry
Cc: php-db@lists.php.net
Subject: Re: [PHP-DB] Arrays from forms

Gary E. Terry wrote:
 If I print them, there's nothing. all three
 
 Here is the same file, in test mode... 
 
 ?php
 include db.inc.back.php;
 
 
 foreach ($_FILES[pictures][error] as $key = $error) {
if ($error == UPLOAD_ERR_OK) {
echo $error_codes[$error];
move_uploaded_file(
  $_FILES[pictures][tmp_name][$key],
  bikeimages/ .$_FILES[pictures][name][$key]
) or die(Problems with upload);
${image.$key} = $_FILES['pictures']['name'];
}
 }

Hmm, looks ok.

If you:

print_r($_FILES[pictures]);

and

print_r($_FILES[pictures]['error']);

do you get stuff there ?

-- 
Postgresql  php tutorials
http://www.designmagick.com/


smime.p7s
Description: S/MIME cryptographic signature


Re: [PHP-DB] Arrays from forms....

2006-11-30 Thread Chris

Gary E. Terry wrote:

Nope...  here is the output.

-- -- Array ( [error] = )


So files aren't being uploaded properly at all.

Does your form have:

enctype=multipart/form-data

in the form header?

ie

form name='blah' action='form2.php' enctype=multipart/form-data


Are your picture fields files?

Upload file: input type=file name=pictures[]

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DB] Arrays from forms....

2006-11-30 Thread Gary E. Terry
OK.. I am a moron.. Forgot that I had changed the name of the field in the
form, and got sidetracked and didn't change it in the
php... But, still not working correctly.

Here is the output: Array -- Array -- Array



 from:



?php
include db.inc.back.php;


foreach ($_FILES[pictures][error] as $key = $error) {
   if ($error == UPLOAD_ERR_OK) {
   echo $error_codes[$error];
   move_uploaded_file(
 $_FILES[pictures][tmp_name][$key],
 bikeimages/ .$_FILES[pictures][name][$key]
   ) or die(Problems with upload);
   ${image.($key+1)} = $_FILES['pictures']['name'];
   }
}

$make = $_POST['make'];
$model = $_POST['model'];
$year = $_POST['year'];
$color = $_POST['color'];
$description = $_POST['description'];
//$image1 = $_POST['image1'];
//$image2 = $_POST['image2'];
//$image3 = $_POST['image3'];
$status = $_POST['status'];

echo $image1 -- $image2 -- $image3;

/*
$query = INSERT INTO `inventory` (`make`, `model`, `year`, `color`,
`description`, `image1`, `image2`, `image3`, `status`)
VALUES ('$make', '$model', '$year', '$color', '$description',
'$image1', '$image2', '$image3', '$status');;

$result = mysql_db_query($db, $query) or displayErrorQuery ($query);

echo meta http-equiv='refresh' content='0;URL=inventorylist.php';
exit;
*/
?


smime.p7s
Description: S/MIME cryptographic signature


Re: [PHP-DB] Arrays from forms....

2006-11-30 Thread Chris

Gary E. Terry wrote:

OK.. I am a moron.. Forgot that I had changed the name of the field in the
form, and got sidetracked and didn't change it in the
php... But, still not working correctly.

Here is the output: Array -- Array -- Array


Oops this:

${image.($key+1)} = $_FILES['pictures']['name'];

should be:

${image.($key+1)} = $_FILES['pictures'][$key]['name'];


--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DB] Arrays from forms....

2006-11-30 Thread Gary E. Terry
I know you really meant :

${image.($key+1)} = $_FILES['pictures']['name'][$key];


And it works!!! 

Thanks oh so very much!

-Original Message-
From: Chris [mailto:[EMAIL PROTECTED] 
Sent: Thursday, November 30, 2006 11:46 PM
To: Gary E. Terry
Cc: php-db@lists.php.net
Subject: Re: [PHP-DB] Arrays from forms

Gary E. Terry wrote:
 OK.. I am a moron.. Forgot that I had changed the name of the field in the
 form, and got sidetracked and didn't change it in the
 php... But, still not working correctly.
 
 Here is the output: Array -- Array -- Array

Oops this:

${image.($key+1)} = $_FILES['pictures']['name'];

should be:

${image.($key+1)} = $_FILES['pictures'][$key]['name'];


-- 
Postgresql  php tutorials
http://www.designmagick.com/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



smime.p7s
Description: S/MIME cryptographic signature


Re: [PHP-DB] Arrays from forms....

2006-11-30 Thread Chris

Gary E. Terry wrote:

I know you really meant :

${image.($key+1)} = $_FILES['pictures']['name'][$key];


Oh yeh ;)

Glad we got there in the end! :)

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php