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...

Комментарии • 28

  • @DigitalFox-tutorials
    @DigitalFox-tutorials  2 года назад +3

    Hello guys, if you have any questions you can leave a comment, or you can just say HI :)

    • @patrickzeigler4391
      @patrickzeigler4391 2 года назад

      How would you change the php code to handle the JSON being posted from an application rather than from a file?

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  2 года назад +1

      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.

    • @Lullabyt
      @Lullabyt 2 года назад +2

      @@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.

    • @patrickzeigler4391
      @patrickzeigler4391 2 года назад +2

      @@Lullabyt Thank you for that response! Got up and running!!!

    • @Lullabyt
      @Lullabyt 2 года назад

      @@patrickzeigler4391 Great!!

  • @Lullabyt
    @Lullabyt 2 года назад +1

    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!!

  • @laravelearner
    @laravelearner Год назад +1

    beautiful.............. simple, concise yet descriptive. THANK YOU

  • @kalyanipriyadarsini
    @kalyanipriyadarsini 2 года назад +1

    Nice explanation

  • @rakibulhasanroki4089
    @rakibulhasanroki4089 5 дней назад

    Shown success buy my db table is still empty😔
    What can i do for solve this problem?

  • @leonardoribeiro7706
    @leonardoribeiro7706 2 года назад +1

    Thanks!

  • @peterjozsa5699
    @peterjozsa5699 Год назад

    Thank you, bro 🙂

  • @user-yi6tl9eg7u
    @user-yi6tl9eg7u 11 месяцев назад

    nice

  • @Jizeppi
    @Jizeppi Год назад

    Whenever I run the file it says success but my dB table is empty

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Год назад +2

      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.

    • @laravelearner
      @laravelearner Год назад

      @@DigitalFox-tutorials u a life saver

    • @husniamrizal1353
      @husniamrizal1353 Год назад +1

      @@DigitalFox-tutorials I have tried the data from you script, but the database is still empty

    • @hamededasd
      @hamededasd Год назад

      I have the same problem

    • @StudioOnePhotographyMichigan
      @StudioOnePhotographyMichigan 4 месяца назад

      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.