kenwvs Posted August 6, 2006 Share Posted August 6, 2006 I have a form written in PHP that the user fills out the form and can also upload images at the same time. Once the form is submitted, the form fields are uploaded to a MySQL Database and the images are suppose o upload to a separate directory. The problem I am having is that I can set it up so either the data is uploaded, or the images are uploaded, but they won't both get uploaded. I have tried changing the names or the submit buttons around, and even if they are the same, only one or the other will upload.if (!isset($_POST['Submit'])){ is the one line and I have changed the word submit to action to match up with the other line which reads[code]if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>"; print "<form method='post' enctype='multipart/form-data'>"; for($i=0;$i<$numoffile;$i++) { print "<input type='file' name='myfiles[]' size='30'><br>"; } print "<BR><BR> <input type='submit' name='action' value='Submit Work Order'>";[/code]I have also tried changing the word action to submit (both with a capital and small "s") and again, one or the other will upload.Is there any way to get them both to upload, or do I need to go back to the drawing board to figure out a way to upload 3 - 4 images with the form?here is the complete form code:[code]<?phpinclude_once "myconnect.php";?><?php/* WorkOrder.php */if (!isset($_POST['Submit'])){?><head><link href="ge.css" rel="stylesheet" type="text/css"><title>GE Work Order</title></head><form action="" method="post" enctype="multipart/form-data"><center><div>Work Order #:<input type="text" size="12" maxlength="8" name="Work" value=<?php if (isset($_POST['Work'])) {echo $_POST['Order'];} else {echo date("mHis");}?>>Date:<input type="text" size="12" maxlength="12" name="Sched" value=<?php if (isset($_POST['Sched'])) {echo $_POST['Date'];} else {echo date("Y-m-d");} ?>><?php$result = mysql_query("SELECT * FROM employees") or die(mysql_error());echo "Technician Name: <select name='Tech'>";while($row=mysql_fetch_array($result)){ echo"<option>$row[Name]</option>";} echo"</select>";?></select><BR></><BR></>Site:<select name="Site"><option value="No Selection Made">Select Site</option><option value="Burdett">Burdett</option><option value="Coaldale">Coaldale</option><option value="Fort McLeod">Fort McLeod</option><option value="Taber">Taber</option></select>Unit Serial #:<input type="text" size="10" maxlength="10" name="Serial">Unit Hours:<input type="text" size="6" maxlength="6" name="Hours"><BR></><BR></>Unit Starts:<input type="text" size="6" maxlength="6" name="Starts">Severity:<select name="Severe"><option value="No Selection Made">Please Select Level</option><option value="Low Level">Low Level</option><option value="A.S.A.P.">A.S.A.P.</option><option value="Immediate Attention">Immediate Attention</option><option value="Severe">Severe</option></select> Safety:<select name="Safe"><option value="No Selection Made">Please Select Level</option><option value="Low Risk of Injury">Low Risk of Injury</option><option value="Medium Risk of Injury">Medium Risk of Injury</option><option value="High Risk of Injury">High Risk of Injury</option><option value="Injury has Occurred">Injury has Occurred</option></select></div> <BR><div><B>Description of Issue:</B><BR><textarea rows="4" name="Issue" cols="116"></textarea></div><BR><div><B>Possible Resolution</B><BR><textarea rows="4" name="Resolve" cols="116"></textarea></div><BR><div><B>List of Parts Required (known at this time)</B>:</div><div><input type="text" size="155" maxlength="155" name="PartsA"><BR><input type="text" size="155" maxlength="155" name="PartsB"><BR><input type="text" size="155" maxlength="155" name="PartsC"><BR><input type="text" size="155" maxlength="155" name="PartsD"><BR><input type="text" size="155" maxlength="155" name="PartsE"><BR><input type="text" size="155" maxlength="155" name="PartsF"><BR><input type="text" size="155" maxlength="155" name="PartsG"><BR></div><BR><div>Request for Assistance:<BR><textarea rows="4" name="Assist" cols="116"></textarea></div><BR><BR><div><div><center><B>Send this form to:<BR><?php$result = mysql_query("SELECT * FROM employees") or die(mysql_error());echo "<select name='Tech'multiple>";while($row=mysql_fetch_array($result)){ echo"<option>$row[Name]</option>";} echo"</select>";?></select><BR><div>Hold The Control Key down to select multiple names</b></div><BR><BR><!--Work Order # <?php $_POST["Work"];?> was opened on <?php $_POST["Sched"];?> by<?php $_POST["Tech"];?>.<br><br>The Unit is located in<?php $_POST["Site"]?> with serial number <?php $_POST["Serial"]?>and this unit has <?php $_POST["Hours"]?> Hours on it.The Reported level of Severity is<?php $_POST["Severe"];?>and the Safety Level is<?php $_POST["Safe"];?>. <BR>Additional Details on this equipment failure are as follows:<BR><BR><B>Description of Issue:</B><BR><?php $_POST["Issue"]?><BR><BR><B>Possible Resolution</B><BR><?php $_POST["Resolve"]?><BR><BR><B>Request for Assistance:</B><BR><?php $_POST["Assist"]?><BR><BR>Parts Required: (that the technician is currently aware of)<?php $_POST["PartsA"]?><BR><?php $_POST["PartsB"]?><BR><?php $_POST["PartsC"]?><BR><?php $_POST["PartsD"]?><BR><?php $_POST["PartsE"]?><BR><?php $_POST["PartsF"]?><BR><?php $_POST["PartsG"]?><BR> --></div><BR><BR><?php /*********************************************** * Snippet Name : File Uploader * * Scripted By : Hermawan Haryanto * * Website : http://hermawan.com * * Email : hermawan@codewalkers.com * * License : GPL (General Public License) * ***********************************************/ $numoffile = 5; // Fix path of your file to be uploaded, don't forget to CHMOD 777 to this folder $file_dir = "/wamp/www/BenProject/uploadfile/"; if ($_POST) { for ($i=0;$i<$numoffile;$i++) { if (trim($_FILES['myfiles']['name'][$i])!="") { $newfile = $file_dir.$_FILES['myfiles']['name'][$i]; move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile); $j++; } } } if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>"; print "<form method='post' enctype='multipart/form-data'>"; for($i=0;$i<$numoffile;$i++) { print "<input type='file' name='myfiles[]' size='30'><br>"; } print "<BR><BR> <input type='submit' name='action' value='Submit Work Order'>"; //"<input type="submit" name="submit" value="Submit Work Order">" <BR>?></form><?php} else{$Work = $_POST["Work"];$Sched = $_POST["Sched"];$Name = $_POST["Tech"];$Site = $_POST["Site"];$Serial = $_POST["Serial"];$Hours = $_POST["Hours"];$Starts = $_POST["Starts"];$Issue = $_POST["Issue"];$Severity = $_POST["Severe"];$Resolution = $_POST["Resolve"];$Assistance = $_POST["Assist"];$PartsA = $_POST["PartsA"];$PartsB = $_POST["PartsB"];$PartsC = $_POST["PartsC"];$PartsD = $_POST["PartsD"];$PartsE = $_POST["PartsE"];$PartsF = $_POST["PartsF"];$PartsG = $_POST["PartsG"];$Safety = $_POST["Safe"];mysql_query("INSERT INTO `work`(Work, Sched, Name, Site, Serial, Hours, Starts, Issue,Severity, Resolution, Assistance, PartsA, PartsB, PartsC, PartsD, PartsE, PartsF, PartsG, Safety)VALUES ('$Work', '$Sched', '$Name', '$Site', '$Serial', '$Hours', '$Starts', '$Issue','$Severity', '$Resolution', '$Assistance','$PartsA', '$PartsB', '$PartsC', '$PartsD','$PartsE', '$PartsF', '$PartsG', '$Safety')")or die(mysql_error());echo "Your Work Order has been posted successfully";}?><?php//echo print_r($_POST)?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/ Share on other sites More sharing options...
king arthur Posted August 6, 2006 Share Posted August 6, 2006 How does this script work? I can see two opening form tags but only one closing form tag. Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70116 Share on other sites More sharing options...
kenwvs Posted August 6, 2006 Author Share Posted August 6, 2006 That is probably where the problem is. I had the form working and then added the script to upload images. I may need to remove the one at the top, and then use a hidden attribute. Do you think that would work? Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70119 Share on other sites More sharing options...
king arthur Posted August 6, 2006 Share Posted August 6, 2006 Well you can have two forms on one page, but only the contents of the one that is submitted will get POSTed to the handling script. So if you want the inputted data and the image selection to both be posted, put all the input into the one form. Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70122 Share on other sites More sharing options...
ignace Posted August 6, 2006 Share Posted August 6, 2006 may i ask what the following does?[code]<!--Work Order # <?php $_POST["Work"];?> was opened on <?php $_POST["Sched"];?> by<?php $_POST["Tech"];?>.<br><br>The Unit is located in<?php $_POST["Site"]?> with serial number <?php $_POST["Serial"]?>and this unit has <?php $_POST["Hours"]?> Hours on it.The Reported level of Severity is<?php $_POST["Severe"];?>and the Safety Level is<?php $_POST["Safe"];?>. <BR>Additional Details on this equipment failure are as follows:<BR><BR><B>Description of Issue:</B><BR><?php $_POST["Issue"]?><BR><BR><B>Possible Resolution</B><BR><?php $_POST["Resolve"]?><BR><BR><B>Request for Assistance:</B><BR><?php $_POST["Assist"]?><BR><BR>Parts Required: (that the technician is currently aware of)<?php $_POST["PartsA"]?><BR><?php $_POST["PartsB"]?><BR><?php $_POST["PartsC"]?><BR><?php $_POST["PartsD"]?><BR><?php $_POST["PartsE"]?><BR><?php $_POST["PartsF"]?><BR><?php $_POST["PartsG"]?><BR> -->[/code]if it is your intention that it is being outputted, then i think your missing a few echo's/print's. also from $_POST['issue'] to $_POST['PartsG'] a few ; are also missing, set your error_reporting to error_reporting(E_ALL);also looking at the following, tells me you are someone that really likes typing...[code]$Work = $_POST["Work"];$Sched = $_POST["Sched"];$Name = $_POST["Tech"];$Site = $_POST["Site"];$Serial = $_POST["Serial"];$Hours = $_POST["Hours"];$Starts = $_POST["Starts"];$Issue = $_POST["Issue"];$Severity = $_POST["Severe"];$Resolution = $_POST["Resolve"];$Assistance = $_POST["Assist"];$PartsA = $_POST["PartsA"];$PartsB = $_POST["PartsB"];$PartsC = $_POST["PartsC"];$PartsD = $_POST["PartsD"];$PartsE = $_POST["PartsE"];$PartsF = $_POST["PartsF"];$PartsG = $_POST["PartsG"];$Safety = $_POST["Safe"];[/code]to safe you some time, try the following next time:[code]while (false !== (list($k, $v) = each($_POST))) { ${$k} = $v;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70123 Share on other sites More sharing options...
kenwvs Posted August 6, 2006 Author Share Posted August 6, 2006 Sorry, the first portion isn't in use right now. I am learning, and trying to figure out how to email results to people who are selected through one of the form fields, so it is quite possible that it isn't even close. I am still trying to figure it out, so if I am way off, save me the time and tell me to start over. Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70125 Share on other sites More sharing options...
kenwvs Posted August 6, 2006 Author Share Posted August 6, 2006 Do you have a suggestion as the best way to set this up so they are all in one form? Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70132 Share on other sites More sharing options...
kenwvs Posted August 6, 2006 Author Share Posted August 6, 2006 Using while (false !== (list($k, $v) = each($_POST))) { ${$k} = $v;}Does that just cover everything within what I typed, and prevents errors as well? Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70143 Share on other sites More sharing options...
king arthur Posted August 6, 2006 Share Posted August 6, 2006 [quote author=kenwvs link=topic=103157.msg410597#msg410597 date=1154861217]Do you have a suggestion as the best way to set this up so they are all in one form?[/quote]Remove this line:[code]print "<form method='post' enctype='multipart/form-data'>";[/code]and move this code[code] if ($_POST) { for ($i=0;$i<$numoffile;$i++) { if (trim($_FILES['myfiles']['name'][$i])!="") { $newfile = $file_dir.$_FILES['myfiles']['name'][$i]; move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile); $j++; } } } if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";[/code]from inside the form part of the script to inside the "else" construct where you are reading all the other POSTed values. Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70148 Share on other sites More sharing options...
kenwvs Posted August 6, 2006 Author Share Posted August 6, 2006 Not sure if I moved the one peice to the right spot, so I am showing it below. Neither the image uploads, not the form data are being copied to the database now. I realize the uploads should be going to a directory, but they are not going there.Ken[code]<?php /*********************************************** * Snippet Name : File Uploader * * Scripted By : Hermawan Haryanto * * Website : http://hermawan.com * * Email : hermawan@codewalkers.com * * License : GPL (General Public License) * ***********************************************/ $numoffile = 5; // Fix path of your file to be uploaded, don't forget to CHMOD 777 to this folder $file_dir = "/wamp/www/BenProject/uploadfile/"; print "<form method='post' enctype='multipart/form-data'>"; for($i=0;$i<$numoffile;$i++) { print "<input type='file' name='myfiles[]' size='30'><br>"; } print "<BR><BR> <input type='submit' name='action' value='Submit Work Order'>"; //"<input type="submit" name="submit" value="Submit Work Order">" <BR>?></form><?php} elseif ($_POST) { for ($i=0;$i<$numoffile;$i++) { if (trim($_FILES['myfiles']['name'][$i])!="") { $newfile = $file_dir.$_FILES['myfiles']['name'][$i]; move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile); $j++; } } } if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";{$Work = $_POST["Work"];$[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16695-php-form/#findComment-70154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.