Skip to content
Advertisement

while uploading image in php shwoing error

I have an image upload option in my website, when the user upload the image, it goes to database and it should display in the homepage. I have done the following code:

for add.php

JavaScript

for db.class.php which handles the operation:

JavaScript

but when i am trying to upload the image, its showing

Sorry, there was an error uploading your file

the code which adds the image to table:

JavaScript

as am new to this can anyone please tell me what is wrong in my code, thanks in advance

Advertisement

Answer

It looks like you are trying to insert the image directly into the table. As per my experience till now, it doesn’t work that way.

The basic uploading of file in PHP is done by using the move_uploaded_file function. You can get more details Here.

When you upload a file in PHP through file HTML element then it’ll be uploaded to a temporary location and will provide you the details in the $_FILES array. You can find the complete file information such as file name, size, mime type, temp name and errors in the $_FILES array.

So what you should do is upload the file to a destination folder before inserting it to your database. So you need to create a function for upload:

JavaScript

You’ll have to call this function before your insert query. So your code will be like:

JavaScript

Try this. There might be some minor modifications required in the code according to your logic. I have passed $_FILES['image'] as a parameter but you can also use the $_FILES array directly in the upload function without passing anything to it.

Also please note that you’ll have to use the same logic in the edit function as well but I guess you can do it. If not, let me know.

Hope this helps.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement