mathewjenkinson Posted April 10, 2011 Share Posted April 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/ Share on other sites More sharing options...
wright67uk Posted April 10, 2011 Share Posted April 10, 2011 could you try; mysql_query ("INSERT INTO temperature (field1, field2, field3) VALUES ('value1, 'value2', 'value3')"); Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1199796 Share on other sites More sharing options...
spiderwell Posted April 10, 2011 Share Posted April 10, 2011 $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 Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1199797 Share on other sites More sharing options...
Pikachu2000 Posted April 10, 2011 Share Posted April 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1199809 Share on other sites More sharing options...
mathewjenkinson Posted April 10, 2011 Author Share Posted April 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1199827 Share on other sites More sharing options...
Pikachu2000 Posted April 11, 2011 Share Posted April 11, 2011 have you echoed the query string to be sure it contains the values you'd expect it to contain? Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1199898 Share on other sites More sharing options...
mathewjenkinson Posted April 17, 2011 Author Share Posted April 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1202681 Share on other sites More sharing options...
Pikachu2000 Posted April 17, 2011 Share Posted April 17, 2011 That code has no parse errors. Is that script the one named temperature_logger.php? Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1202693 Share on other sites More sharing options...
mathewjenkinson Posted April 17, 2011 Author Share Posted April 17, 2011 yeah, theirs only 1 script in that directory: its called temperature_logger.php and i post the variables to it via temperature_logger.php?device=001&temp=23 Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1202698 Share on other sites More sharing options...
dcro2 Posted April 17, 2011 Share Posted April 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1202702 Share on other sites More sharing options...
mathewjenkinson Posted April 17, 2011 Author Share Posted April 17, 2011 i stripped off the path the whole error is: PHP Parse error: syntax error, unexpected T_VARIABLE in /Library/WebServer/Documents/domain.com/temperature_logger.php on line 3 Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1202709 Share on other sites More sharing options...
Pikachu2000 Posted April 17, 2011 Share Posted April 17, 2011 Are you sure the file is actually uploading, and to the correct location? As I said, there's no parse error in the code you posted . . . Quote Link to comment https://forums.phpfreaks.com/topic/233303-sql-query-not-inserting-data/#findComment-1202716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.