//initialize connection to MySQL database require_once('../test_db_pdo.php'); $tablename = 'log_table'; $userip = $_SERVER['REMOTE_ADDR']; $timestamp = date('Y-m-d H:i:s'); //grab query params/values $devicename = isset($_GET['device_name']) ? $_GET['device_name'] : ''; $value1 = isset($_GET['value1']) ? $_GET['value1'] : ''; //$value1 = rand(10,100); //temp test if ($devicename == '' || $value1 == ''){ //send update message back to arduino to set led or whatever displaying the entry was successful echo "error"; //(missing data) //whatever message/format you want }else{ //check to see if device already has entry in db or if its a 'newcomer' to the playground $deviceCheck_sql = "SELECT record_id FROM $tablename WHERE device_name = :device_name LIMIT 1"; $deviceCheck_qryparams = array( ':device_name' => $devicename ); $deviceCheck_stmt = $conn->prepare($deviceCheck_sql); $deviceCheck_stmt->execute($deviceCheck_qryparams); //$row = $deviceCheck_stmt->setFetchMode(PDO::FETCH_ASSOC); $deviceCount = $deviceCheck_stmt->rowCount(); if ($deviceCount > 0) { //device exists in database //update device states/status/sensor value $logEntry_sql = "UPDATE $tablename SET time_stamp=:time_stamp, value1=:value1, ip_address=:ip_address WHERE device_name=:device_name"; } else { //device does nto exist in database //insert device state/status/sensor value $logEntry_sql = "INSERT INTO $tablename (time_stamp, device_name, value1, ip_address) VALUES(:time_stamp, :device_name, :value1, :ip_address)"; } $logEntry_qryparams = array( ':time_stamp' => $timestamp, ':device_name' => $devicename, ':value1' => $value1, ':ip_address' => $userip ); $logEntry_qry = $conn->prepare($logEntry_sql); $results = $logEntry_qry->execute($logEntry_qryparams); //send a response back to Arduino if desired if($results){ //send update message back to arduino to set led or whatever displaying the entry was successful echo "success";//whatever message/format you want }else{ //send update message back to arduino to set led or whatever displaying the entry was successful echo "fail";//whatever message/format you want //db post/insert (log entry) failed. //echo "
The log entry failed.
" . var_dump($logEntry_qry ->errorInfo()); } } ?>