1

I'm using php function uniqid() for creating unique identifier for every order.

<?php $order_id=uniqid(); ?>

And below in form:

<input type="hidden" name="ordercode" value="<?php echo $order_id; ?>">

After submitting form, all data is recorded in mysql database, and user is redirected to payment. It's working ok, but: If I press "back" button in browser and return to order page, input named ordercode contains the old value! This is unwanted because "ordercode" must be unique in database to recognize the order when client will make payment. If I submit the form again, there will be 2 records in database with same ID. I guess this is caused by browser cache. If so, is there any way to force browser to refresh page when going back? Or is there another cause? Any suggestions?

Julia
  • 1,251
  • 1
  • 11
  • 24
  • http://stackoverflow.com/questions/2530/how-do-you-disable-browser-autocomplete-on-web-form-field-input-tag – Bogdan Burym Aug 31 '15 at 15:18
  • 1
    The page is probably being served from the cache. Why don't you check if the ID already exists, instead of blindly insert it? – Charlotte Dunois Aug 31 '15 at 15:22
  • @Charlotte Dunois I was hoping on uniqid uniqueness :) – Julia Aug 31 '15 at 15:26
  • @Julia You can't do anything if the page is being served from the cache. :) You could use `AUTO INCREMENT` with `PRIMARY` INDEX, that would do the job. – Charlotte Dunois Aug 31 '15 at 15:27
  • The problem is that same ID must be sent in 2 places: in database and to payment system. To database it's going with ajax, and to payment system with direct POST method. If ID will be generated with autoincrement, it will be unique in database but how to send it to payment system in the moment when user submits order :( – Julia Aug 31 '15 at 15:33
  • I'm also thinking about generating ID with JQuery every time when submit button is pressed – Julia Aug 31 '15 at 15:35
  • `uniqid()` is NOT guaranteed to be unique in the first place, so it is possible that it would cause a conflict even if the user doesn't hit the back button and submit again. Anyways, have you considered generating the ID after the submission (serverside)? – The Unknown Dev Jan 21 '16 at 18:53

1 Answers1

-1

there is simple way to achieve that , you can mark this column ordercode as uniqu from database

ALTER TABLE table ADD UNIQUE(ordercode);

ahmadMarafa
  • 1,248
  • 1
  • 13
  • 15
  • When it marked as unique, new record with same ordercode causes mysqli error and record doesn't save at all – Julia Aug 31 '15 at 15:24
  • unique column is used to prevent duplicated entry , so any time , user insert same unique id inside database , there will be an error or , you can just check for duplicated entry before insert , – ahmadMarafa Aug 31 '15 at 15:44
  • I can check for duplicates. But how can it help me if same ID is already sent to payment system? Also I can just update the record in database if ID is same, but it is prefered to save all orders without rewriting them. Also i'm not sure what will happen if user will make payment and after that will go back and make another order. – Julia Aug 31 '15 at 16:01