How to insert data from a json file in the database | PHP, JSON, MYSQL Tutorial
HTML-код
- Опубликовано: 12 сен 2024
- For this tutorial we have some products stored in a JSON file.
We are going to see how to fetch the raw data from the JSON
file, convert them in a php array, and insert them in a database.
We are also take a quick look of prepared statements so our database
will stay safe from the risk of an SQL injection.
You can read the article on my website, where you can copy the source code.
digitalfox-tut....
If you feel like saying thanks, you can buy me a coffee.
www.buymeacoff...
Hello guys, if you have any questions you can leave a comment, or you can just say HI :)
How would you change the php code to handle the JSON being posted from an application rather than from a file?
If you have an API key, you will send an http request with CURL to fetch the data and store them in a json file, and then in the database. Hope that is what you asked.
@@patrickzeigler4391 I was dealing with the same sittuation Patrick. There goes how I finally managed to get things going:
First step:
File: entry.php (receives data from another application/server, sets a random name and saves it as .json)
Second step:
File: insert_into_db.php (takes data from generated .json and inserts into database)
I needed to abandon PDO in this case because my dedicated's server PHP was outdated. But it's working out fine this way because I can trust the sending source as data is being generated by a device, not other people, so no risks of malicous interference.
In my case a .JSON is generated every 30 seconds. Still I got to figure out how to take the latest file generated in the folder in order to set in the database and then create a routine to call insert_into_db.php every 20 seconds, for example, in order to always insert the latest without skipping older ones. But this is something I'm gonna work out tomorrow. For now I'm gonna get some sleep. I can share the solution later once I find it if someone needs in the future.
@@Lullabyt Thank you for that response! Got up and running!!!
@@patrickzeigler4391 Great!!
You just saved my life... I'm working in receiveing data from a tracker device directly from my local host server to my front end in the dedicated server. Data is flowing through JSON. I was struggling with how to store them in the database. I'm very grateful. Information clearly passed without even speak!! Keep up the great content & job!!
Thank you
beautiful.............. simple, concise yet descriptive. THANK YOU
Nice explanation
Shown success buy my db table is still empty😔
What can i do for solve this problem?
Thanks!
Thank you, bro 🙂
Welcome 😊
nice
Thanks
Whenever I run the file it says success but my dB table is empty
Hey, download the source code from here:
digitalfox-tutorials.com/tutorial.php?title=How-to-insert-JSON-data-in-the-database
Change the database login details in the php file, and un-comment the execute() method in line 24.
@@DigitalFox-tutorials u a life saver
@@DigitalFox-tutorials I have tried the data from you script, but the database is still empty
I have the same problem
Me as well! hey, please help us out, your code seems legit, but some of us need a little help where we went wrong. I am getting succes" echo but nothing is inserted to database, and no database connection errors.