Note that there are some explanatory texts on larger screens.

plurals
  1. POImage upload form not working when I use enctype="multipart/form-data"
    text
    copied!<p>I am having issues trying to get this image upload to work. It isn't working whether I use enctype='multipart/form-data' or not but when i am not using it the page will show me my errors, when I add enctype='multipart/form-data' to the form tag it does nothing at all when I click submit. Does anyone see any problems? Any help is appreciated!</p> <p>Below is the upload code:</p> <pre><code>//define a maxim size for the uploaded images in Kb define ("MAX_SIZE","100"); //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } if ($submit == "Add" &amp;&amp; $_SERVER['REQUEST_METHOD'] == 'POST' &amp;&amp; $product_name != "" &amp;&amp; $product_image != "" &amp;&amp; $product_description != "" &amp;&amp; $product_price != "" &amp;&amp; $product_dimensions != "" &amp;&amp; $product_category != "" &amp;&amp; $product_subcategory != "" &amp;&amp; $product_manufacturer != "" &amp;&amp; $product_toronto_avail != "" &amp;&amp; $product_mississauga_avail != "" &amp;&amp; $product_concord_avail != "" &amp;&amp; $product_pickering_avail != "" &amp;&amp; $product_barrie_avail != "" &amp;&amp; $product_kitchener_avail != "") { // Create Query $addQuery="INSERT INTO products (name, image, description, price, dimensions, category, subcategory, manufacturer, toronto, mississauga, concord, pickering, barrie, kitchener) VALUES ('$product_name', '$product_image', '$product_description', '$product_price', '$product_dimensions', '$product_category', '$product_subcategory', '$product_manufacturer', '$product_toronto_avail', '$product_mississauga_avail', '$product_concord_avail', '$product_pickering_avail', '$product_barrie_avail', '$product_kitchener_avail')"; // Run Query &amp; Add into products table mysql_query($addQuery); //reads the name of the file the user submitted for uploading $image= $product_image; //if it is not empty if ($image) { //get the extension of the file in a lower case format $extension = getExtension($image); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests if (($extension != "jpg") &amp;&amp; ($extension != "jpeg") &amp;&amp; ($extension != "png") &amp;&amp; ($extension != "gif")) { //print error message echo '&lt;h1&gt;Unknown extension!&lt;/h1&gt;'; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server $size=filesize($image); //compare the size with the maxim size we defined and print error if bigger if ($size &gt; MAX_SIZE*1024) { echo '&lt;h1&gt;You have exceeded the size limit!&lt;/h1&gt;'; } //we will give an unique name, for example the time in unix time format $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="../gallery/".$image_name; //we verify if the image has been uploaded, and print error instead $moved = move_uploaded_file($image_name,$newname); if (!$moved) { echo '&lt;h1&gt;Copy unsuccessfull!&lt;/h1&gt;'; echo $newname . '&lt;br/&gt;'; echo $image_name; }}} //Successful! $successAdd = "&lt;p style='font-size:12px;font-style:italic;font-family:georgia;'&gt;You have successfully added \"&lt;span style='color:#014380;'&gt;" . $product_name . "&lt;/span&gt;\" to your products database.&lt;/p&gt;"; } else if ($submit == "Add" &amp;&amp; $_SERVER['REQUEST_METHOD'] == 'POST' &amp;&amp; $product_name == "" || $product_image == "" || $product_description == "" || $product_price == "" || $product_dimensions == "" || $product_category == "" || $product_subcategory == "" || $product_manufacturer == "" || $product_toronto_avail == "" || $product_mississauga_avail == "" || $product_concord_avail == "" || $product_pickering_avail == "" || $product_barrie_avail == "" || $product_kitchener_avail == "") { $successAdd = "&lt;p style='font-size:12px;color:red;font-style:italic;font-family:georgia;'&gt;Please fill in all required fields. &lt;/p&gt;"; } else { $successAdd = ""; } </code></pre> <p>&amp; below is the form code:</p> <pre><code>echo "&lt;form method='post' enctype='multipart/form-data' action='./add_product.php' &gt;"; echo "&lt;table border='0' class='addProductTable' cellpadding='3' cellspacing='0'&gt;"; print ("&lt;tr class='tableHeader large'&gt; &lt;th style='width:200px;'&gt;&lt;/th&gt; &lt;th style='width:175px;'&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Name:* &lt;/td&gt; &lt;td&gt; &lt;input type='text' name='name' /&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Upload an Image:* &lt;/td&gt; &lt;td&gt; &lt;input type='file' name='image' /&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Description:* &lt;/td&gt; &lt;td colspan='2'&gt; &lt;textarea name='description' cols='25' rows='5'&gt;&lt;/textarea&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Price:* &lt;/td&gt; &lt;td&gt; &lt;input type='text' name='price' /&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Dimensions:* &lt;/td&gt; &lt;td&gt; &lt;input type='text' name='dimensions' /&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Category:* &lt;/td&gt; &lt;td&gt; &lt;input type='text' name='category' /&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Subcategory:* &lt;/td&gt; &lt;td&gt; &lt;input type='text' name='subcategory' /&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Manufacturer:* &lt;/td&gt; &lt;td&gt; &lt;input type='text' name='manufacturer' /&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Availability:* &lt;/td&gt; &lt;td colspan='2'&gt; &lt;div style='float:left; margin-right:10px;'&gt; &lt;span&gt;Toronto:&lt;/span&gt; &lt;input type='text' style='width:30px;' name='toronto_availability' /&gt; &lt;br/&gt; &lt;span&gt;Mississauga:&lt;/span&gt; &lt;input type='text' style='width:30px;' name='mississauga_availability' /&gt; &lt;br/&gt; &lt;span&gt;Concord:&lt;/span&gt; &lt;input type='text' style='width:30px;' name='concord_availability' /&gt; &lt;br/&gt; &lt;/div&gt; &lt;div style='float:left;'&gt; &lt;span&gt;Pickering:&lt;/span&gt; &lt;input type='text' style='width:30px;' name='pickering_availability' /&gt; &lt;br/&gt; &lt;span&gt;Barrie:&lt;/span&gt; &lt;input type='text' style='width:30px;' name='barrie_availability' /&gt; &lt;br/&gt; &lt;span&gt;Kitchener:&lt;/span&gt; &lt;input type='text' style='width:30px;' name='kitchener_availability' /&gt; &lt;br/&gt; &lt;/div&gt; &lt;div style='clear:both;'&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt;"); echo "&lt;/table&gt;"; echo "&lt;input type='submit' name='submit' value='Add' /&gt;"; echo "&lt;/form&gt;"; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload