从nodemcu发布数据时如何避免编码错误

时间:2019-08-03 17:48:54

标签: php sql database arduino gps

获取错误

注意:未定义的索引:第17行的C:\ xampp \ htdocs \ Real-Time-master \ server \ gps-2-map \ api \ index.php中的device_id

注意:未定义的索引:第18行的C:\ xampp \ htdocs \ Real-Time-master \ server \ gps-2-map \ api \ index.php中的date_and_time

注意:未定义的索引:第19行的C:\ xampp \ htdocs \ Real-Time-master \ server \ gps-2-map \ api \ index.php中的纬度

注意:未定义索引:第20行上的C:\ xampp \ htdocs \ Real-Time-master \ server \ gps-2-map \ api \ index.php中的经度

// 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

1 个答案:

答案 0 :(得分:0)

发生错误是因为在验证之前您处理了空数据。尝试将变量声明更改为此

$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;