How to save data in 'DataStore (local storage)' to MySQL Table?

Hello, again guys,

I’m a tad stuck. So, I followed the tutorial on how to create a shopping cart with the data store, worked super nice. Thanks a lot for that guys.

Anyway, I ran into a tiny issue, so, now I have my data in the data store, I want to pass to MYSQL database table, I don’t know how to store the repeat data into MySQL database?

Can anyone please advice Thanks a lot.

I have used the Pay Now button in the Checkout page as a trigger to post a form. The form contains hidden inputs with the values obtained from the Data Store as in

Remember that the Data Store stores the information as an array, meaning that you will need a multiple insert database action.

Edit: I have just updated the Git repository using Data Store. You can find it here: https://github.com/Wappler/wCart. If you do not want to install the project, open _checkout.html and go to the bottom of the page to see the relevant section.

1 Like

Remember that the shopping cart is running client-side and can be manipulated by the client. You should never use the prices from the shopping cart in your checkout process. You should use the product ids and quantities from the shopping cart and get the product prices from your database on checkout.

4 Likes

@ben Thanks a lot. I will look into this and feedback in a moment.
@patrick Fantastic Insight. Thank you.

So it worked like a charm. Thanks.

In my wCart project, I have included a security check, that if the Data Store is fiddled with, the checkout page will not be operative. Please try it out for yourself with feedback if the security is strong enough. https://github.com/Wappler/wCart

I haven’t checked the implementation but if the check is done also on client it can be bypassed.

There is no such thing as client-side security. It is an oxymoron

If you are actually calling server and making some checks there then that would be ok.

But you would still have the problem of price changes and stock availability. So if eventually you need to compare local storage against the server for those two things why not do it for everything except for product id and quantity as @patrick suggested.

1 Like

Thanks for clarifying Jon. In my example, I do not have a serverside payment system, I am working on that. I am sure that the validation will exist there. For the moment, I have used the client side to check for the correct values. I have even multiplied the (sub)totals by the value of varCheckoutCheck, which, when the values do not match, will result in 0. A zero payment means that the product will not be sent by the seller (after they have checked the invoice) and on the database side, the transaction will not be activated. Instead I have sent them to a wayward URL. Please check https://github.com/Wappler/wCart/blob/master/_checkout.html or better still, try the project out and give me your feedback. I can only learn.

1 Like

Well the payment system doesn’t have much to do with securing your checkout process. It is just the last step of the shopping process which includes the checkout.

The local storage(in the shopping cart context) is just used for one reason: to remember info to the user on their device. But that is just about it. Shouldn’t be used as source of truth.

Price, description, stock availability are things that should be stored on server-side. That can be rows in the database, a json file stored in the server, etc.

When retrieving the cart on the browser you should make a call to the server to verify latest price, description and stock availability and update local storage if needed to reflect the latest data. But what’s more important is that the information sent to the payment gateway has to come from the server(database, json file). Never directly from the browser.

In wappler terms. Do not checkout directly datastore information. Send datastore information(id, quantity) to server connect and checkout data retrieved by server connect from your database/jsonfile/etc (id_price*quantity)

I will check the project as soon as possible :slight_smile: I saw your video and looked pretty good.

1 Like

In addition to Jon’s explanation i’d like to add that when you work with (for example) Stripe API you do everything server-side, so everything is done in the server action - i.e. in a PHP file on your server and this can not be manipulated.
So for the checkout process, you just send only your products IDs and quantities stored in the data store (local storage) to your server action - where you can filter a query and use its results with the payment provider’s API. The prices will always be correct there :slight_smile:

I should probably just update the tutorial so it shows how to send this data back to the server action, and how to filter the query, then how to access the results in the server action and then you can add a payment provider API of your choice.

6 Likes

The update would be invaluable @Teodor, looking forward to it!

Hey Teodor. How you send the data from the data store to a server action. I want to retrieve the prices from the database but can not find out how to pass data store values to the server. Have tried with a repeat but I am not getting anything back. Thank you!

You can try this custom formatter: How to : Pass an Array to a Server Action to Write to the Database
Data stores are designed to work on client side and not to be sent to server.

Thank @sid I think I got it using a form to post the data and run through the query. Thank you very much for the link.

A new “Form Data” component was added to the latest Wappler version and it makes sending complex data structures to the server side really easy: