Yii prevent re-inserting data to database from uploaded file on refresh - php

I have an upload form, where I upload csv files and then insert all rows to the database. If I hit refresh, all content is inserted again and again into the database. What is the best prectice to prevent this?
Thanks a lot!
BR

The best way to prevent this behavior is to use a redirect to show the landing page. When a redirect is made the browser history records another url after the post action. in Yii you can achieve this by doing the following from the controller.
$this->redirect("../path/to/action");

You can add a hidden input element to the form which holds a unique ID:
<input type="hidden" name="random" value="4b3403665fea6">
If you store this value in the database you can make sure that no CSV file will be uploaded with the same unique ID (after refresh).
A unique ID can be generated with a built-in PHP function uniqid()

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

Is there a way I can set the value of a file upload so it doesn't whipe on page refresh

Before my page is submitted, PHP is used to verify the integrity of all my data. I am currently using if(isset($_POST['bannerTitle'])){ echo htmlentities($_POST['bannerTitle']);} to keep the set values of my input data fields, but when the page is redrawn my uploaded file data is naturally reset to null.
Is there anything I can do to keep the selected users selected img file?
file uploads doesnt store any post data by default, but you can use $_FILE['fieldname']['name'] to retrieve its name and store at your database.

Transfering a uploaded file from page to page

My client wants to have a 3 page form. The first page allows the user to enter data including a uploaded file. the second page confirms this data. and the third page submits the data to the database and directories.
Via post, I can keep saving the data to a hidden input fields, thats no problem. My problem is the uploaded file. how do I hold that document from page to page ? I am using Cakephp but any advice would help, thanks
You can always just create the illustion that the form is utilising three different pages. Use AJAX to accept and validate/request the user confirm their submitted data. If in this view they accept it initiate a POST to submit all that data.
You really don't need three physically different files to achieve this but you can still let it appear in three stages to keep your client happy.
You just upload the file to temp directory and keep the value in hidden variables just like other form data . If form successfully submitted then the image copy to desired location other wise delete the image
You can easily fake these 3 pages using CSS. Or even 2, as "third page" is actually a server script which has nothing to do with pages in the browser.
Just make your form, then put an event on the submit button which changes divs to whatever "confirmation page" he wants. and then actually send the form using a button on this page.
that's all
An uploaded file is always held temporarily. The server env var should tell you where it is. In Ruby's rack it is stored in the params var. So I guess there is a similar params var in php which has a hash with all the necessary information.
Since the file would be uploaded on the first step, one option is to put the file's location in a hidden input field along with the rest of the data (either there, or put it in the session). With CakePHP, if your file field looks somewhat like that:
<input type="file" name="data[User][image]" id="UserImage" />
Then you will be able to capture the location through
$location = $this->data['User']['image']['tmp_name'];
Which will correspond to something like /var/tmp/xxxxxx
On the last page, if the user confirms all the data, you just use move_uploaded_file() to put the file wherever you want on the server.
move_uploaded_file($location, '/new/location');

Inserting and retrieving id before form is submitted

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

Php page refresh , get the post values from formm

i got a jquery upload and crop script and i am trying to use it.
First i have a 1.html file which has a form, which requires some texts and image. After submitting the form it goes to main.php where it checks for some image properties and if successful it refreshes the page using header("location:".$_SERVER["PHP_SELF"]);
So if i place my $_POST['name'] i get the value from 1.html . Now when the image in displayed after page refresh there is one more option to select the thumbnail and upon selecting the thumbnail there is one more page refresh, to display the final images (both bigger and thumb). Now my problem is for second page refresh i am not able to get the fields which i had posted from 1.html. Any suggestion would be highly appreciated. Thanks
With that header refresh you are losing every information. Drop the refresh and do consecutive forms: you need to propagate the values you need from the first form, using hidden input fields in the forms that follow
<input type="hidden" value="<?php echo $value_from_original_post; ?>">
or you can store the value(s) of interests in session variable(s).
Alternatively, you can use an AJAX solution which requires no reload or page change, but it's a bit more work (and you might not want javascript).
You can not store states between pages unless:
Keep rolling the value(s) forward as hidden input type if it's a form submission.
Temporarily save value(s) as cookie until consumed.

Categories