Jump to content

Recommended Posts

hi guys :)

 

Ive written this php to take in two variables from the http POST, the idea is that I can multiple devices submit temperature readings to the php script, the script then append the unix time stamp and then the the 3 variables - device id, temp and unix time are then stored in a mysql DB.

 

I can get the variables to present on a php page for debugging but I cant get the variables to be stored in the mysql DB.

 

See the code:

 

<?php

$unixtime = time();
// get device variables
$device_id=$_GET['device'];
$device_temp=$_GET['temp'];

/* 
//for testing purposes
echo "unixtime: " . $unixtime . "<br />";
echo "device id: " . $device_id . "<br />";
echo "device_temp: " . $device_temp . "<br />";
*/

// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

//mysql query
$query = "INSERT INTO temperature VALUES ('',$device_id,$temp,$unixtime)";

// Insert a row of information into the relevant device table
mysql_query($query);
or die(mysql_error());  

mysql_close();
echo "Data Inserted!";


?>

 

I cant see where Im going wrong to correct this, but as nothing is displayed on the page i believe I am not forming the query correctly?

- any ideas would be much appreciated.

 

Thank you

Mathew

Link to comment
https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/
Share on other sites

$query = "INSERT INTO temperature VALUES ('',$device_id,$temp,$unixtime)"; doesnt list the columns but should still work but to be sure its going into the right ones it might be worth putting them into the sql

 

$query = "INSERT INTO temperature (`id`,`deviceid`,`temperature`,`unixtime`) VALUES ('',$device_id,$temp,$unixtime)";

 

what data types are the columns and do they match the data you are trying to insert ? integer into integer, date into date etc

When you say nothing is displayed, I have to assume you mean just a blank screen. You have a fatal parse error caused by the line-terminating semicolon between mysql_query() and or die(). If you had error_reporting set up properly, it would have informed you of that via a message to the effect of "unexpected T_LOGICAL_OR" with the line number.

Hi Pikachu2000,

 

I got access to the error log 5 mins ago, it displays a 'T_LOGICAL_OR' error.

 

the mysql query now looks like:

 

//mysql query
$query = "INSERT INTO temperature (`id`,`deviceid`,`temperature`,`unixtime`) VALUES ('',$device_id,$temp,$unixtime)";

// Insert a row of information into the relevant device table
mysql_query($query);  

 

the php script now echos that data has been inserted however when i go into the DB via phpmyadmin, theirs nothing in the table temperature.

from the looks of things the mysql query is still wrong?

thanks,

Mathew

 

Hi all,

 

I went away and re-examined my code.

I have two dev environments - one local and one remote.

 

The code:

<?php
 
$unixtime = time();
// get device variables
$device_id=$_GET['device'];
$device_temp=$_GET['temp'];
 
/*
//for testing purposes
echo "unixtime: " . $unixtime . "<br />";
echo "device id: " . $device_id . "<br />";
echo "device_temp: " . $device_temp . "<br />";
*/
 
// Make a MySQL Connection
mysql_connect("localhost", "root", "swordfish") or die(mysql_error());
mysql_select_db("logger") or die(mysql_error());
 
//mysql query
$query = "INSERT INTO temperature (`id`,`device_id`,`temp`,`unixtime`) VALUES ('',$device_id,$device_temp,$unixtime)";
 
// Insert a row of information into the relevant device table
mysql_query($query);
 
mysql_close();
echo "Data Inserted!";
 
 
?>

works perfectly fine on my local server but the moment i upload it to my remote server. which has the same config I get the following error:

 

 

PHP Parse error:  syntax error, unexpected T_VARIABLE in /temperature_logger.php on line 3

 

after a quick google a few websites have suggested that i am missing the ; from line 3.

but I have that. so It cant be that.

 

 

Im very baffled as to why I can get it to work on my local machine and not on my remote server?

Any ideas?

Thanks

Mathew

 

Are you sure the directory where web documents go is correct? And where you're uploading is correct? Because the error says /temperature_logger.php. That would mean it's in the disk's root (/) folder.

Typically these files are under some form directory like /var/www/ or /home/username/etc/.

 

Unless you just stripped off the path. Or you have some really weird host.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.