Team:TU-Munich/TUM13 save.php

From 2013.igem.org

Revision as of 17:08, 28 October 2013 by FlorianA (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
<?php

// include db interface and init
include 'lib/db.inc.php';

// Initialize array for values
$data = array();

// Check for key
if ( isset($_GET['key']) ) {

	// Check for the right key
	if ( $_GET['key'] == '7edd7340' ) {
		
		// Save time
		if ( isset($_GET['time']) ) {
			$data['time'] = &$_GET['time'];
		}
		
		// Save temperature
		if ( isset($_GET['temp']) ) {
			$data['temperature'] = &$_GET['temp'];
		}
		
		// Save water
		if ( isset($_GET['water']) ) {
			$data['water'] = &$_GET['water'];
		}
		
		// Save light-vis
		if ( isset($_GET['light1']) ) {
			$data['light-vis'] = &$_GET['light1'];
		}
		
		// Save light-ir
		if ( isset($_GET['light2']) ) {
			$data['light-ir'] = &$_GET['light2'];
		}
		
		// Save photo1
		if ( isset($_GET['photo1']) ) {
			$data['photo1'] = &$_GET['photo1'];
		}
		
		// Save photo2
		if ( isset($_GET['photo2']) ) {
			$data['photo2'] = &$_GET['photo2'];
		}
		
		// Save photo3
		if ( isset($_GET['photo3']) ) {
			$data['photo3'] = &$_GET['photo3'];
		}
		
	} else {
		echo 'Wrong key received!';
	}
	
} else {
	echo 'No key received!';
}

// Check for values
if ( isset($data['time']) and ( sizeof($data) > 1 ) ) {

	// Prepare SQL query
	$sql = "INSERT INTO\n\tarduino_data(";
	
	$i = 0;
	foreach ($data as $k => $v) {
		if ($i != 0) $sql .= ', ';
		$sql .= "`" . $k . "`";
		$i++;
	}
	
	$sql .= ")\nVALUES\n\t(";
	
	$i = 0;
	foreach ($data as $k => $v) {
		if ($i != 0) $sql .= ', ';
		if ($k == 'time')
			$sql .= "'" . date('Y-m-d H:i:s',$v) . "'";
		else
			$sql .= mysqli_real_escape_string($db, $v);
		$i++;
	}
	
	$sql .= ')';
  
  // echo $sql;
  
  if (!$db->query($sql)) {
  	echo 'Data writing failed!' . $db->error;
  } else {
  	echo 'Values ';
  	$i = 0;
  	foreach ($data as $k => $v) {
  		if ($i != 0) echo ', ';
  		echo $k;
  		$i++;
  	}
  	echo ' successfully written!';
  }
	
} else {
	echo 'No time and or no values received!';
}

?>