通过php发送时,数据未显示在数据库中

时间:2018-09-01 16:38:30

标签: php sql database arduino xampp

我是php和数据库处理的新手。我已经使用PHP和XAMPP完成了将数据从一个arduino传感器发送到数据库的工作。我的问题是从多个传感器发送数据。

文件“ write_data_w2”中的PHP代码

<?php

$dbusername = "w123";  
$server = "localhost"; 

$dbconnect = mysqli_connect($server, $dbusername);
$dbselect = mysqli_select_db($dbconnect,"weather1");


$sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET["temperature"].",".$_GET["humidity"].",".$_GET["rain"]."')";    


mysqli_query($dbconnect, $sql);
?>

我没有为用户“ w123”使用密码。

我想检查所有内容,并尝试通过浏览器使用 “ http://localhost/write_data_w2.php?temperature=32&humidity=45&rain=N

没有任何反应,没有警告,没有错误,没有数据。数据库保持为空。

数据库名为“ weather1”,由1个名为“ weather”的表和5个名为“ id”,“ time”,“ temperature”,“湿度”,“ rain”的列组成。


已解决

根据用户的建议,我添加了以下行:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

显示了一些我后来解决的错误。

我还必须对“ $ sql”进行一些修改:

$sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET['temperature']."', '".$_GET['humidity']."', '".$_GET['rain']."')";

1 个答案:

答案 0 :(得分:1)

只是一个建议

您应该避免直接在sql中使用var或$ GET / POST值的用户,否则有注入SQL的风险,无论如何,您应该添加$ mysqli_error meggage来检查错误。

  $dbusername = "w123";  
  $server = "localhost"; 

  $dbconnect = mysqli_connect($server, $dbusername);
  $dbselect = mysqli_select_db($dbconnect,"weather1");


  $sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET["temperature"].",".$_GET["humidity"].",".$_GET["rain"]."')";    


  mysqli_query($dbconnect, $sql);

  // for check the erro  try add  


  if (!$mysqli_query(dbconnect, $sql)) {
      printf("Errormessage: %s\n", $mysqli_error);
  }

?>