i'm facing an issue that i'm unable to solve. I've changed my coding approach 3 times in a row. It's basically like this:
A user uploads images, these images get uploaded to a local folder + their names in a database where i then fetch them and load them.
A user may edit his images, I've done this through checkbox selection and a button called "Delete selected images". When the button is pressed, an post request through ajax is sent and the new images list is updated into the database and in the folder too without refreshing the page.
My issue is here: Say i had in the database these images: 0.png,1.png,2.png and i deleted the third image 2.png. The database field will be updated to : 0.png,1.png but then if i delete one more image let's say the first image, the database field should now be: 1.png but instead it becomes 1.png, 2.png,this is happening because the variable containing the images is not being updated from the database after every delete. Let's say $row['images'] was fetched from the database but it is not getting updated.
I need help on how to update my php variables from my database after sending the post request with ajax.
Related
I have a html page, with a form and image upload,
I want to update the database with the images once uploaded, the two form work well individually, but I need to link the form-data to the uploaded images in the database.
kindly help with logic, I am considering using sessions
You can use session as you mentioned or you can updated the relevant record with the next form details.
Say for example, once first form is filled and submited by the user, add those details in database and get the ID of that record.
When the second form is submited by the user, update the same record which you have already added for form 1.
So in database you will have only one record for both the forms with all relavent detials.
You could save your images in the session if you don't want to save them in the database (which, by the way, might not be a good idea), this avoids putting stuff in the database that is not yet finally processed (as I understand: this happens when the other form is submitted?)
You could put them in a table for "temporary images" which are not yet processed finally and just keep track of the IDs through the session
I can get my head around the best way to sort this problem.
I have a form that creates a new record in the database (using php and MySQL) this is all fine, but as part of this form I want the user to be able to upload some files. These files would be stored in another table within the DB (allowing multiple entries) and linking back to the original record.
I could upload the images on the next page, after the main record has been created, but really want this nice and simple, all on one form.
Another issue is that if the images are loaded into a temp place, before form submission, I don't want this images staying there forever if the form is left, or cancelled.
I am using Fine uploader to handle the file management, and php to save and place.
Why don't you use a temporary table / directory with a peridodic check-and-delete-old script?
Create a temp table with (id, identifier_key, data, timestamp), and delete old records from it periodiaccly using a cron script (or from your main web script, using a probability, like php's default session handler works - once in a 1000 request or something similar.)
I am working on a CMS in Codeigniter and it has the usual fields like title, post content, etc (the page is similar to Wordpress in this way).
I have an image uploader on the page that saves the image via ajax to a MySQL table with basic fields such as image_id, path, size.
There is another table that stores the relationship between an item and an image that requires both an item_id and an image_id.
The problem I have is that the item_id is not known until the page is created/saved. So if a user goes to the Create Post page and uploads an image from there, I can't add the relationship since I don't have an item_id yet.
Some things I am considering:
Every time someone goes to the Create Post page, it automatically creates a blank post in the MySQL table with just the item_id and storing it in a hidden field. The problem I have with this is the database could get filled with a lot of empty posts if a person just clicks Create Post but never fills it out.
Saving the images in the images table, but also save an array of all of their ids for when the Create button is clicked. When that button is click, then add the image relationships.
Hide the image uploader until the post title is filled out, which triggers a page save
Wordpress seems to have solved this but I'm not sure what it does?
Anyone have any better suggestions?
I would say to structure your images table so that it doesn't require a reference to a specific post. Put image-post relationships in a separate table. On your "compose post" screen, have the image uploader return the ID of the newly-inserted image, and when you submit the post, add the new post ID and the image ID to the table for image-post relationships. This also gives you more flexibility to do things like link to one image in multiple posts.
-- edit: upon re-reading, it sounds like this is already how you're storing them. If so, then there shouldn't be a problem. Just add the image-post relationship at the time the post is submitted. If you're worried about orphaned images, see below. --
You're right that this could lead to orphan images, if someone uploads an image and then closes the window without making a post. I wouldn't worry about the images table "filling up," though. MySQL tables can have millions of rows and still perform well.
If you're concerned about collecting unused image files, you could have a periodic maintenance job that removes image files (and the associated table entries) if the upload date is older than, say, 1 week, and the image isn't associated with any posts.
Seems simple enough:
Use 1 form
Collect the data posted
Insert article - record insert id
Insert Image - record image id
Use ids to insert into relationship table
EDIT
As you are using ajax to upload the image, you could use a return value to load the inserted image id into a hidden form field
I have one form on the page for addition and it is related with 2 tables in the database.
Eg. Say i have a table named event and one table named eventphotos. The uploadify plugin of jquery for eventphotos works just fine, however, event is the master table and eventphotos is the child table. Thus when i upload the photo due to asynchronous behaviour of uploadify the photo gets submitted on server but there is no EventId present for this photo which throws exception when i try to insert the photo into evenphotos. I hope you got the idea what the problem is.
I want that when the user clicks on the submit button on the form, the photo uploading process should start and it should only get inserted into database once the master record is inserted into table.
How do i make this happen?
Thanks in advance :)
One solution: Save the image somewhere in a temorary folder using a random generated name, then send this random name back and insert into your form as hidden field. When the form gets submitted you check if the file still exists and load it.
Another solution: Create a table called "temporary_photos" or similar, insert your picture. And send back the ID of the record and insert it as hidden field in your form. Don't forget timestamps so that you can delete images which have never been used. And delete records if you have saved the event!
Here is what i'm trying to do:
I have a form with few text fields and image upload via JavaScript(to have that loading animation).
here is the link http://netfecs.com/inprogress/phat_cat/admin_edit_car.php?id=4
I'm using row id as part of the image name in the database.
What is a good way to set the id and retrieve if before submitting the form because while user is filling out the form the images are being uploaded and they need an id.
Any Thoughts?
The best solution for you would be to have all the images uploaded stored in a temp location. Store that location in a session. Once the user submits the form, add the data into the database then move all the files in the session to a perm location and write to the database as needed.
I see two ways:
Make an AJAX/JSON call and create the record, returning the id
Create an empty record before displaying the form and use its id in the form