vishalonne Posted November 8, 2012 Share Posted November 8, 2012 Hi I just wan to to pass the value of Input Type File html tag to a 2nd PHP page where I will insert the image in mysql but I am always getting a notice and isset() is not getting the $_FILE('IMAGE'). Here is the notice - Notice: Undefined index: IMAGE in C:\xampp\htdocs\billing\prodinsert.php on line 11 This is my HTML TAGS - <body> <hr /> <form id="form1" name="form1" method="post" action="prodinsert.php" enctype="multipart/form-data"> <input name="ICODE" type="text" size="10" maxlength="6" /> <input name="DESCR" type="text" size="50" maxlength="45" /> <input name="RATE" type="text" size="10" maxlength="9" /> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <input name="IMAGE" type="file" /> </form> And this is my PHP code- <?php $host="localhost"; $user="root"; $pass=""; $db="bill"; mysql_connect($host, $user, $pass) OR DIE (mysql_error()); mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error()); $code=$_POST['ICODE']; $descp=$_POST['DESCR']; $rate=$_POST['RATE']; $image=$_POST['IMAGE']; if(!isset($_FILES[$image])) { echo '<p>Please select a file</p>'; echo $image; } else { echo "File Uploaded"; echo $image; } Where I am making mistake ??? Please guide me. Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/ Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 You cant simply post an image. You use $_FILES["IMAGE"]["name"] You also do not want to upload files to a database, use a directory Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391144 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 You cant simply post an image. You use $_FILES["IMAGE"]["name"] You also do not want to upload files to a database. use a directory Thank you for Guiding me What is the 2nd paprameter ["name"] Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391147 Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 The name of the file itself example: name --> picture.jpg <-- name Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391148 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 The name of the file itself example: name --> picture.jpg <-- name Now I change my code to like this -> if(!isset($_FILES["IMAGE"])) { echo '<p>Please select a file</p>'; echo $image; } else { echo "File Uploaded"; It is now working showing File Uploaded but notice is still coming Notice: Undefined index: IMAGE in C:\xampp\htdocs\billing\prodinsert.php on line 11 Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391150 Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 $image=$_POST['IMAGE']; Change that to $image = $_FILES["IMAGE"]["name"]; Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391151 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 Change that to $image = $_FILES["IMAGE"]["name"]; Now it more problematic If I don't select any image then also it is showing File Uploaded Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391152 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 Now it more problematic If I don't select any image then also it is showing File Uploaded YES YES ITS DONE Thank you Very much NOTICE GONE BUT I am getting Please select a File message and The name of the File Here is the modified code - $code=$_POST['ICODE']; $descp=$_POST['DESCR']; $rate=$_POST['RATE']; $image=$_FILES["IMAGE"]["name"]; if(!isset($_FILES[$image])) { echo '<p>Please select a file</p>'; echo $image; } else { echo "File Uploaded"; echo $image; ...... Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391153 Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 You're recalling the $image variable inside the $_FILES. Change if(!isset($_FILES[$image])) to if(!isset($_FILES["IMAGE"])) Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391156 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 You're recalling the $image variable inside the $_FILES. Change if(!isset($_FILES[$image])) to if(!isset($_FILES["IMAGE"])) CAn I just check the this by this way if(!isset($image)) Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391157 Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 Not sure. $image will always be set because you're giving it a value. You can first try checking if the form was submitted. Then give it a value and it should work Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391158 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 CAn I just check the this by this way if(!isset($image)) As you suggested me to change if(!isset($_FILES[$image])) TO if(!isset($_FILES["IMAGE"])) now if submit the form without selecting any image file it is showing File Uploaded Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391159 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 I just want to save the image of an item in mysql database Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391162 Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 You said that was the problem before *facepalm* Ok try this... $code=$_POST['ICODE']; $descp=$_POST['DESCR']; $rate=$_POST['RATE']; $image=$_FILES["IMAGE"]["name"]; if($_FILES["IMAGE"]["error"] > 0) { echo "Error: " . $_FILES["IMAGE"]["error"] . "<br />"; } else { if(!isset($_FILES["IMAGE"]["name"])) { echo '<p>Please select a file</p>'; } else { echo "File Uploaded"; echo $image; } Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391163 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 You said that was the problem before *facepalm* Ok try this... $code=$_POST['ICODE']; $descp=$_POST['DESCR']; $rate=$_POST['RATE']; $image=$_FILES["IMAGE"]["name"]; if($_FILES["IMAGE"]["error"] > 0) { echo "Error: " . $_FILES["IMAGE"]["error"] . "<br />"; } else { if(!isset($_FILES["IMAGE"]["name"])) { echo '<p>Please select a file</p>'; } else { echo "File Uploaded"; echo $image; } Thank you for your co operation After using the code you gave If I select image it it shows "File Uploaded" with the name of the image file And if I don't select image file it show Error: 4 Can't understand why 4? why not it shows "Please select a file" Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391170 Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 Error code 4 means that there was no file uploaded A full list can be found at http://php.net/manua...load.errors.php If you wish to change it simply do: $code=$_POST['ICODE']; $descp=$_POST['DESCR']; $rate=$_POST['RATE']; $image=$_FILES["IMAGE"]["name"]; if($_FILES["IMAGE"]["error"] > 0) { // create a list of errors here if($_FILES["IMAGE"]["error"] == "4") { echo "Please select a file."; } } else { echo "File Uploaded"; echo $image; } Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391171 Share on other sites More sharing options...
vishalonne Posted November 8, 2012 Author Share Posted November 8, 2012 Error code 4 means that there was no file uploaded A full list can be found at http://php.net/manua...load.errors.php If you wish to change it simply do: $code=$_POST['ICODE']; $descp=$_POST['DESCR']; $rate=$_POST['RATE']; $image=$_FILES["IMAGE"]["name"]; if($_FILES["IMAGE"]["error"] > 0) { // create a list of errors here if($_FILES["IMAGE"]["error"] == "4") { echo "Please select a file."; } } else { echo "File Uploaded"; echo $image; } Great You are Genius Thank you very much Link to comment https://forums.phpfreaks.com/topic/270471-why-i-am-not-able-to-pass-the-value-of-input-type-file/#findComment-1391172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.