Saving to a table when an ID is not yet known - php

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

Related

How do I upload images in php after I have already saved data from the form1 and images are uploaded in form2

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

Need to update data from my database to a php variable

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.

ManyToOne relationship where the child entity serves for AJAX file uploading

I have two entities: Image and Post, that are linked by a ManytoOne relationship. The entity Image is used to handle file upload and store upload relative data (like absolute path...). I am using Symfony2 Cookbook tutorial dealing with file uploads.
I am now building a form that allows a user to:
Enter some post specific informations (like title, content..)
upload with jQuery/AJAX many images for the post.
Send the the whole form by button click.
I am still not finding the right approach to implement this solution regarding data persistence in the database.The problem for me is:
In the entity Image, an attribute ($post_id) is used as Foreign Key and will store the post id. The user will upload many images before the entity post is persisted. All the instances of Image created each time will not contain a value for post_id. In my opinion, performance will be affected if I:
update all the rows in image table after persisting an instance of Post.
Create an empty Post instance first, use its id in the Image instances, then update Post instance.
Any suggestions are highly appreciated.
Why don't you use this way of working with your forms?
http://symfony.com/doc/current/cookbook/form/form_collections.html
And in this way you only upload the images with the entire form.
If you need to preview the photos as the user selects them in the file inputs than you can check this answer
How to preview image before uploading in jquery
But it will not work on older browsers like IE8, IE9.

Upload pictures and make them show together with my post

I need help with a function so that I can upload a picture from my custom CMS, then have it shown together with my post. Look at the picture below:
I want a picture in this image placeholder
I'm not so good at PHP or Mysql so please give me a easy answer.
I'll post my code over here at pastebin, easier for you to watch over there! http://pastebin.com/gBVe9sZV
If you'd like to show a set of images with your post, you'll need to store those images in a table along with the post id that they are associated with.
Upon submitting your form you're probably handling the post data via $_POST. Your images will come across under $_FILES. Once you have performed an insert on your post table, you can call mysql_insert_id() to get the new primary key ID for that post.
The post id can be stored in your images table along with references to your various images. I'm assuming here that you aren't generating your own post id's, however if you are you'd use that instead.
See also Handling File Uploads, on php.net.
Whenever you're querying your database to show the post to your readers, perform a JOIN on the images table based upon a common post id, and you'll have the paths to all related images.
See also Understanding JOINs in MySQL and Other Relational Databases, on sitepoint.com.

How to start uploading photo when the user clicks submit button?

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!

Categories