• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

More than one table in the database

Q&A Forum › Category: Other › More than one table in the database
0 Vote Up Vote Down
Rweng009 asked 4 years ago

The project examples are excellent for one station and one table. Plenty of tinkering available.
However in a more complex database with several tables (recording several stations)  simplistically with say sensors in one table and readings in another
 
How to set up PHP to receive the data and send it to the correct table?
I have tried permutations of your wx stn code and transactions but just cant get it to work.
Any assistance appreciated

Question Tags: PHP & MySql
5 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Hello, I hope you are doing well.
This is a though question to answer, because it really depends on your project requirements. In summary:

  1. First, you need to start by learning a bit about SQL and learn how to setup a table with all the fields that you want to store in your database.
  2. Then, you need to write a PHP script that will receive the data and insert it into your database
  3. Your ESP will send the data to your PHP script based on the defined parameters

I hope this helps!
Rui

0 Vote Up Vote Down
Rweng009 answered 4 years ago

Hullo Rui and thank you for the time to respond.
I am fine setting up the tables and structure in SQL – it is getting the information into them that i struggle with.
I am sure it cant be too hard but havent found the right website to get me there yet.
As a simple example  let us say table 1 has three variables with a times stamp and table 2 has another three variables with a time stamp.
var1, var2, var3 for table 1 
var4, var5, var6 for table 2.
var’s 1-6 are read from the one device.
 how do i export those variables from the device (one long string?) and get them in to the correct table thru PHP. 
 Any pointers appreciated.

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

It’s a bit hard to explain in a few sentences, learning PHP is a full course on itself, but I highly recommend this website:

  • http://www.w3schools.com/php/default.asp

I have this sample project about PHP and MySQL: ESP32/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE
This script receives the readings from the ESP and inserts them in the database.

  • https://raw.githubusercontent.com/RuiSantosdotme/ESP32-ESP8266-PHP-MySQL/master/code/post-esp-data.php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
 $api_key = test_input($_POST["api_key"]);
 if($api_key == $api_key_value) {
  $sensor = test_input($_POST["sensor"]);
  $location = test_input($_POST["location"]);
  $value1 = test_input($_POST["value1"]);
  $value2 = test_input($_POST["value2"]);
  $value3 = test_input($_POST["value3"]);

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

  $sql = "INSERT INTO SensorData (sensor, location, value1, value2, value3)
VALUES ('" . $sensor . "', '" . $location . "', '" . $value1 . "', '" . $value2 . "', '" . $value3 . "')";

You could receive it like this:
$var1= test_input($_POST["var1"]);
$var2= test_input($_POST["var2"]);
$var3= test_input($_POST["var3"]);
$sql = "INSERT INTO table1 (var1, var2, var3) VALUES ('" . $var1 . "', '" . $var2 . "', '" . $var3 . "')";

It would be something like that… Then your ESP would make an HTTP POST request with var1, var2, var3 variables in the request.

  • https://raw.githubusercontent.com/RuiSantosdotme/ESP32-ESP8266-PHP-MySQL/master/code/ESP32_ESP8266_MySQL_Database_PHP.ino

Or you can learn more about HTTP: ESP32 HTTP GET and HTTP POST with Arduino IDE (JSON, URL Encoded, Text) https://randomnerdtutorials.com/esp32-http-get-post-arduino/

0 Vote Up Vote Down
Rweng009 answered 4 years ago

Dear Rui
Thank you for your patience – I have followed that example previously with many experimental permutations (it works fine for one table). I have tried modifying it by the addition of another “insert” into a different table with / without an additional loop but I cannot get that to work. Some investigation suggests that it wont and I need to do a transaction using BEGIN…INSERT… COMMIT I have tried this as well without success. Other investigation suggest that just good PHP programming will do it without the need for a transaction.?? I do understand the concepts but struggle with the programming for what i understand should be a relatively common event.
 Kind regards

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

But did you create a new table in your database? Is it created?
If it is, you need to insert data in your new table.

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.