Home > Software engineering >  Notice: Undefined index: temperature
Notice: Undefined index: temperature

Time:01-27

I'm new to php and I am currently following a tutorial about how to send values from the NODEMCU to MYSQL database, on the tutorial it seemed to be working fine, but when I tried it this error showed up. I need guidance.

<?php
class thesis_final{
 public $link='';
 function __construct($temperature, $heartrate){
  $this->connect();
  $this->storeInDB($temperature, $heartrate);
 }
 
 function connect(){
  $this->link = mysqli_connect('localhost','root','') or die('Cannot connect to the DB');
  mysqli_select_db($this->link,'example') or die('Cannot select the DB');
 }
 
 function storeInDB($temperature, $heartrate){
  $query = "insert into thesis_final set heartrate='".$heartrate."', temperature='".$temperature."'";
  $result = mysqli_query($this->link,$query) or die('Errant query:  '.$query);
 }
 
}
if (isset($_GET['temperature'] != '' and  $_GET['heartrate'] != '')){
 $thesis_final=new thesis_final($_GET['temperature'],$_GET['heartrate']);
}
?>

CodePudding user response:

Try isset($_GET['temperature']), note the brackets closing the $_GET parameter.

Sometimes its also easier to store the parameter in a variable so you can re-use it and create tidier code, so you can try:

$temperature = $_GET['temperature'] ?: 'N/A';

This is ternary operator and it will check if the parameter exists, and if not it will place 'N/A' in it.

  •  Tags:  
  • Related