使用PHP将数据发布到多个列到Mysql数据库中

时间:2015-04-11 23:46:41

标签: php mysql post arduino

所以我正在尝试使用PHP为我的高级设计项目从Arduino Mega向MySQL数据库发送3个数据值,但我遇到了一个问题。我找到了一个带有1个数据值的示例,该数据值也会在收到数据时为其打印时间戳。对于我的生活,我无法弄清楚如何更改PHP代码以将3个值传递到3列。以下是通过以下方式发送的一个数据值的PHP脚本:

<?php

foreach ($_REQUEST as $key => $value)
{
	if ($key == "yourdata") {
		$yourdata = $value;
 

}




// Check Connection to Database
if (mysql_connect($localhost, $username, $password))
  {
  	@mysql_select_db($database) or die ("Unable to select database");

    // Next two lines will write into your table 'test_table_name_here' with 'yourdata' value from the arduino and will timestamp that data using 'now()'
    $query = "INSERT INTO $tablename VALUES ($yourdata)";
  	$result = mysql_query($query);
  } else {
  	echo('Unable to connect to database.');
  }

?>	

这就是我认为适用于3个值但我们最终无法工作的内容:

<?php

foreach ($_REQUEST as $key => $value)
{
	if ($key == "yourdata") {
		$yourdata = $value;
	}
        if ($key === "yourdata1"){
		$yourdata1 =  $value;
	}

	if ($key === "yourdata2){
		$yourdata2 = $value;


}


   

// Check Connection to Database
if (mysql_connect($localhost, $username, $password))
  {
  	@mysql_select_db($database) or die ("Unable to select database");

    // Next two lines will write into your table 'test_table_name_here' with 'yourdata' value from the arduino and will timestamp that data using 'now()'
    $query = "INSERT INTO $tablename(yourdata, yourdata1, yourdata2) VALUES ($yourdata, $yourdata1, $yourdata2)";
  	$result = mysql_query($query);
  } else {
  	echo('Unable to connect to database.');
  }

?>	

我尝试通过输入网址中的地址进行测试

http://hydrosen.byethost11.com/insert_mysql1.php?yourdata=23&yourdata1=43&yourdata2=555

就像我说的,如果我尝试使用一个数据值,它可以工作,但它不适用于三个值。该表的列名是“yourdata”“yourdata1”和yourdata2“。 任何有关这个令人沮丧的问题的帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

试试这个:

$yourdata = "";
$yourdata1 = "";
$yourdata2 = "";

foreach ($_REQUEST as $key => $value)
{
    if ($key == "yourdata") {
        $yourdata = $value;
    }
        if ($key === "yourdata1"){
        $yourdata1 =  $value;
    }

    if ($key === "yourdata2){
        $yourdata2 = $value;
}

// Next two lines will write into your table 'test_table_name_here' with 'yourdata' value from the arduino and will timestamp that data using 'now()'
    $query = "INSERT INTO $tablename('yourdata', 'yourdata1', 'yourdata2') VALUES ('$yourdata', '$yourdata1', '$yourdata2')";

答案 1 :(得分:0)

好的,这里。 Connect.inc.php有凭据信息和函数.php只是将数据路由到我认为的另一个表。我不确定是否所有这些都是发布数据所必需的,但我的朋友写了所有附加代码,我们&#39我一直在努力让项目的其余部分工作,所以我还没有完全理解它是如何工作的。

<?php
//ob_start();
include_once 'functions.php';
include_once 'connect.inc.php';
$id =2; // Id of sensor
$yourdata = "";
$yourdata1 = "";
$yourdata2 = "";
foreach ($_REQUEST as $key => $value)
{
    if ($key == "yourdata") {
        $yourdata = $value;
    }
        if ($key === "yourdata1"){
        $yourdata1 =  $value;
    }

    if ($key === "yourdata2"){
        $yourdata2 = $value;
}
$tot_data= $yourdata.','. $yourdata1.','.$yourdata2.';';}
//$tot_data= $yourdata.','. $yourdata1.','.$yourdata2.';';
if ($select_stmt = $connection->prepare("SELECT `sensor_id`, `wifidata` FROM `wifi` WHERE `sensor_id` = ? ")) 
    {
//$id = $_SESSION['user_id'];
        $select_stmt->bind_param('d', $id);
        $select_stmt->execute();    // Execute the prepared query.
            $res = $select_stmt->get_result();
                if($res->num_rows==0)
        {
            if ($insert_stmt = $connection->prepare("INSERT INTO 
                `wifi`(`sensor_id`,`wifidata`)
                VALUES(?,?) ")) 
            {