-1

getting error

Notice: Undefined index: device_id in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 17

Notice: Undefined index: date_and_time in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 18

Notice: Undefined index: latitude in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 19

Notice: Undefined index: longitude in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 20

// Establish a connection to database
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'gps2db');
$db_connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

// Extract POST-data to variables
$device_id = mysqli_real_escape_string($db_connection, $_POST['device_id']);
$date_and_time = mysqli_real_escape_string($db_connection, $_POST['date_and_time']);
$latitude = mysqli_real_escape_string($db_connection, $_POST['latitude']);
$longitude = mysqli_real_escape_string($db_connection, $_POST['longitude']);

// Validate URL - If required parameters are found, continue processing
if(isset($device_id) && isset($date_and_time) && isset($latitude) && isset($longitude))
{
    // Check that device corresponding to device_id aka MAC-address is found from database
 $check_device = mysqli_query($db_connection,"SELECT device_id FROM devices WHERE device_id = '$device_id'");

 // If device corresponding to device_id is found, continue processing
 if(mysqli_num_rows($check_device)) 
 {
  // Find out if there is any existing location information in database for given device
AFSANOX
  • 1
  • 1

1 Answers1

0

Error happen because you process null data before you do validation. Try change variable declaration to this

$device_id = isset($_POST['device_id']) ? mysqli_real_escape_string($db_connection, $_POST['device_id']): null;
$date_and_time = isset($_POST['date_and_time']) ? mysqli_real_escape_string($db_connection, $_POST['date_and_time']): null;
$latitude = isset($_POST['latitude']) ? mysqli_real_escape_string($db_connection, $_POST['latitude']): null;
$longitude = isset($_POST['longitude']) ? mysqli_real_escape_string($db_connection, $_POST['longitude']): null;
William Gunawan
  • 748
  • 3
  • 8
  • it show 404 Not found . Notice: Undefined index: device_id in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 17 Notice: Undefined index: date_and_time in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 18 Notice: Undefined index: latitude in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 19 Notice: Undefined index: longitude in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 20 – AFSANOX Aug 04 '19 at 06:40
  • try again. i have edited the code. – William Gunawan Aug 04 '19 at 06:50