[PHP-DB] Arrays from forms....
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
Re: [PHP-DB] Arrays from forms....
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] 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
RE: [PHP-DB] Arrays from forms....
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....
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....
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....
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....
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....
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....
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....
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....
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....
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
Re: [PHP-DB] Arrays and forms
Hello list, I submitted this problem earlier but got no response so I thought I'd elaborate. The code below successfully displays all of the problems from the db. Based on what is chosen here, needs to go into another table in the db along with a customer tracking id. for($knt = 0;$row = mysql_fetch_row($res3); $knt++) { $cat_detail = $row[0]; echo trtdinput type=\checkbox\ name=\prob[]\ value = \$cat_detail\/td td $cat_detail /td .td High input type=\checkbox\ name=\level[]\ value=\1\/td .td Med input type=\checkbox\ name=\level[]\ value=\2\/td .td Low input type=\checkbox\ name=\level[]\ value=\3\/td .tdcenterinput type=\checkbox\ name=\yes\ Yes /center/td/tr; } When this page is submitted, I can successfully capture $prob[] - but I am having no luck in pulling the corresponding $level[] (if one was checked). So my form - once submitted - may look like: Problem one (no priority picked) Problem two High priority Problem three Low priority My $prob[] would be:My $level[] would be: $prob[0]: Problem one $level[0]: High $prob[1]: Problem two $level[1]: Low $prob[2]: Problem three So as you can see my second problem does not correctly correspond to the correct priority. The first (or all) element(s) in the level array may be null. I have read up on and tried some associative arrays but no luck in utilizing them within a form; been trying all sorts of testing, books, web tuts, etc. but still come up with the same problem. Also in case anyone were to suggest sessions, my supervisor is adamently opposed to using them on this site, not sure why, not even sure if that would make my life easier or not since I've never used them. Any comments (good, bad, indifferent) here would be most appreciated. Any suggestions as to other ways of doing this also appreciated. Many thanks Mignon -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Arrays and forms
On Wednesday 15 January 2003 03:23, Mignon Hunter wrote: Hello list, I submitted this problem earlier but got no response so I thought I'd elaborate. The code below successfully displays all of the problems from the db. Based on what is chosen here, needs to go into another table in the db along with a customer tracking id. for($knt = 0;$row = mysql_fetch_row($res3); $knt++) { $cat_detail = $row[0]; echo trtdinput type=\checkbox\ name=\prob[]\ value = \$cat_detail\/td td $cat_detail /td .td High input type=\checkbox\ name=\level[]\ value=\1\/td .td Med input type=\checkbox\ name=\level[]\ value=\2\/td .td Low input type=\checkbox\ name=\level[]\ value=\3\/td .tdcenterinput type=\checkbox\ name=\yes\ Yes /center/td/tr; } When this page is submitted, I can successfully capture $prob[] - but I am having no luck in pulling the corresponding $level[] (if one was checked). So my form - once submitted - may look like: Problem one (no priority picked) Problem two High priority Problem three Low priority My $prob[] would be: My $level[] would be: $prob[0]: Problem one $level[0]: High $prob[1]: Problem two $level[1]: Low $prob[2]: Problem three So as you can see my second problem does not correctly correspond to the correct priority. The first (or all) element(s) in the level array may be null. The reason is that: 1) unchecked checkboxes do not make it into php 2) because you're not specifying an index for the array (level[]), php will create it for you automatically So instead of using just name=level[] specify the level explicitly: name=level[0], name=level[1], etc NB you should do the same for prob[] as well. Having done that then in your example you should get something like: $prob[0]: Problem one $prob[1]: Problem two $level[1]: High $prob[2]: Problem three$level[2]: Low NB that $level[0] is undefined. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* Harp not on that string. -- William Shakespeare, Henry VI */ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php