0

I am sending from arduino usinh http 1.1 (x-www-form-urlencoded) temperature and humidity readings to mysql database. if I use commented part of the following code, everything works (tab12 is the name of the table in mysql database). But I would like to arduino send the name of the table, so I could use the same add.php file to play with multiple arduinos. The thing is, I don't understand how to correctly put the table name from $tabid=$_POST["tabid"]; to query.

<?php
include("connect.php");

$link=Connection();

$tabid=$_POST["tabid"];
$temp1=$_POST["temp1"];
$hum1=$_POST["hum1"];

//  $query = "INSERT INTO `tab12` (`temperature`, `humidity`) 
//      VALUES ('".$temp1."','".$hum1."')"; 

$query = "INSERT INTO `"tabid"` (`temperature`, `humidity`) 
    VALUES ('".$tabid."','".$temp1."','".$hum1."')"; 

mysql_query($query,$link);
mysql_close($link);

header("Location: index.php");
?>
Jens
  • 67,715
  • 15
  • 98
  • 113
penezis
  • 3
  • 1
  • 2
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 19 '15 at 13:30

1 Answers1

0

You would concatenate just as you would with any variable:

$query = "INSERT INTO `" . $tabid . "` (`temperature`, `humidity`) 
VALUES ('".$temp1."','".$hum1."')"; 

In addition your number of columns and values must match.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119