Save Arduino masterdata in database - php

I want to save the sensor data in my database.
Therefore I want to do a GET request to a php file where I compare the uuid's. That's just to look if this arduino already exists in the database. If this Uuid does not exist than I want to do a new entry.
Have someone ideas how to realize that?
As I said before, I thought about doing a get request to the php file on my webserver but this wasn't successfull. Beceause I havent a static UUID.
Therefore I need to hardcode it into my arduino and I also dont know how to do that.
PHP-File
Arduino Code

cool project.
I spot several locations where your issues could be coming from.
In your php code you expect the parameters sensorType, batteryState, onlineState, sending, frequency, sensorDateTimeId and value to be available as get parameters,yet in your arduino code, you're not sending any of those, you're only sending a uuid.
in you php code, you're using variable $uuid, yet you're not populating it anywhere.
you're trying to use mysql_real_escape_string on a array, that will lead to a error. Besides, mysql_real_escape_string is deprecated,and even removed from php in php7. you'd better use other libraries to communicate with mysql. you can for instance have a look at this tutorial: http://codular.com/php-mysqli
If you fix all of those errors, you can try your php code first, before moving on to the arduino code. you should be able to try it by just calling the php page from a browser in this way:
http://yourserver/easy2sense/phpfiles/sensor_connection.php?uuid=12345&sensorType=1&batteryState=2&onlineState=3&sending=4&frequency=5&sensorDateTimeId=6&value=7

Related

Pass data from smarty to php

Smarty lets you use PHP tags but for some reason session_start does not work in Smarty. I could not find out how.
I can't pull out a session variable in a PHP file from a smarty template.
What I need is very simple. Pass data from Smarty (TPL) to a php file. Same server, same domain, same host. Some people asked this before but exactly the other way around. Notice that I don't want to pass data from PHP to Smarty, but the other way around.
Using get, post or cookies is unsafe and a database would be overkill since all I need is make sure the USER A is still the USER A when he lands on the php page. Sessions are perfect and they are exactly for this, but since I cannot start a php session in Smarty it seems there is no way to connect them together. Using curl with a post command like this was an external page would very overkill, because both pages are in the same server, and both are executing exactly the same php server, so why is this so hard? Something like this should be simple but it seems its not. Not at least without exposing this to the user with get or post.
Does someone know a safe way to do this or connect them?

How can I get my javascript gererated data from the client to the server via PHP

A while back I wrote a rather long javascript procedure for organizing data we receive at work. The user simply paste in the mess we get and script throws out all the worthless info and generates a nice cleaned up data table.
I would like to add the ability to then transfer the processed information to the mySQL database. I'm growing a bit more comfortable using javascript, but I don't have close to the time or know-how to recreate the long processing procedure in PHP. How should I prep the data in javascript to most efficiently hand off the data to the server and have PHP insert it into mySQL tables?
The less PHP server side the better, although I doubt it would be safe to have a PHP page that blindly followed any instructions a referring page might send it.
At this point the data my script presents in the browser looks a lot like mySQL records already.
ex.
(Wilson, Paul, 1000400, A399)
(Smalls, Kalah, 4993944, B11)
(Chase, Danny, 244422, B133)
(Larson, Jay, 3948489, J39)
...
Thanks!
If you could have the data in a JSON array.
Then on the php side use json_decode to pull the data in as an array loop through it and do your updates and inserts for your data in MySQL.
http://php.net/manual/en/book.json.php

Save a javascript string in the server

I have PHP up to 5.4, Perl 5.8, C and Python available server-side.
So basically I have JavaScript client-side and I'm generating a string. I'd like to save this string server-side.
I know how to load a string from a file in the server with XMLHttpRequest. But I don't like the idea to use a file for just storing a string even if it's a big one.
So what I'm looking is an example from a string which goes from Javascript to PHP (or something available for me) and back from PHP to JavaScript.
If it does not involved MySQL it is better (I just want to save a string).
I have nothing about jQUERY but I'm not planning to look into it right now.
EDIT 1 : To precise what I need, the user will connect to the website play with the JS application and in the middle save some simple stuff (the string). I want that afterward when he'll come back (or another user) to the website he can get his stuff back.
EDIT 2 : since it cannot be done as nnnnnn says in the comment. I changed the question to :
I'd like to save a string during the use of a JS application and restore it for the next user.
EDIT 3 : I'm looking for a simple example for saving the string in a file, as no other solution is feasible.
If I understand correctly:
send needed to server and save it to some temporary place (file, db, whatever)
next load, ask via ajax if there is any change or data saved
if so, load them
if data are javascript sode, you can use eval() code
Another, quite dirty solution is use cookie data
You cannot expect that supported features in new browsers are supported in old ones as well - savind localy is not possible.
As per my understanding, you want to share data between user. So that, you are sending information to the server and server is responsible to give the same data later. And you dont want to use file as well as database. Session is not suitable to keep large data.
Then,
An alternate solution is store your data in Memcache server. This is more suitable in your case. But you have to remember one thing about Memcache, That is, Memcache is not persistent storage. But you can increase Memcache flushing interval as much as you want.
Note: Memcache is RAM consuming process.

Way to send POST data to a PHP file from another PHP file?

Alright, so I've looked at a ton of questions, but I only found 1 that resembled what I am trying to do. Here is the link to it: Passing POST data from one web page to another with PHP
I want to pass data from one PHP file(we'll call it editData.php) to another PHP file(we'll call it submitData.php). Neither file has any HTML elements (pure PHP code I mean). The first file(editData.php) receives $_POST data, edits it, and needs to send it to the second file. The second file(submitData.php) needs to be able to read in the data using $_POST. No sessions nor cookies can be used I'm afraid.
In the linked question above, the answer accepted was to create hidden fields inside a form and POST the data from there. This worked for the OP because he had user interaction on his "editData.php", so when the user wanted to go to "submitData.php", he would POST the data then.
I can't use this solution(at least, I don't think I can), because I am accessing (and sending $_POST data to) editData.php from a javascript AJAX call and there will be no user interaction on this page. I need the modified data to be POSTed by code, or some other way that does the transfer 'automatically'(or 'behinid-the-scenes' or whatever you want to call it). submitData.php will be called right after editData.php.
I don't know if I can rewrite submitData.php to accept GET data, so count that out as well (it's a matter of being able to access the file). I really don't want to echo stuff back to my original JavaScript function(and then AJAX again). I am encrypting info in editData.php, and (while it sounds silly to say it) I don't want to make it easy for someone to develop a cipher for my encryption. Returning values after being encrypted(viewable with Inspect Element) would make it too easy to decipher if you ask me.
I feel like this issue could come up a lot, so I'd expect that there is something obvious I'm missing. If so, please tell me.
tl;dr? How can I send data to a PHP file via the POST method while only using code in another PHP file?
Well you might consider just streamlining your approach and including the submitData logic at the end of the editData file. But assuming that this is not possible for some reason (files live on different systems, or whatver), your best bet might be to use cURL functionality to post the data to the second script.
If the files are on the same server though I would highly recommend not posting the data to the second script as this will basically just double the amount of requests your web server needs to handle related to this script.

PHP Get Cookie by Session ID (or otherwise pass data between two different connections)

Normally I try to format my question as a basic question and then explain my situation, but the solution I'm looking for might be the wrong one altogether, so here's the problem:
I'm building a catalog application for an auction website that has the ability to save individual lots. So far this has worked great by simply creating a cookie with a comma-separated list of IDs for those lots, via something like this:
$_COOKIE["MyLots_$AuctionId"] = implode(",",$arrayOfIds);
The problem I'm now hitting is that when I go to print the lots, I'm using wkhtmltopdf through the command-line to request the url of the printout I want, like this:
exec("wkhtmltopdf '$urlofmylots' filename.pdf");
The problem is that I can't pass a cookie to this call, because Apache sees an internal request, not the request of the user. I tried putting it in the get string, but once I have more than a pre-set limit for GET parameters, that value disappears from the $_GET array on the target url. I can't seem to find a way to send POST data between them. My next possible ideas are the following:
Maybe just pass the sessionID to the url, and see if there's a way that I can use PHP to dig through the cookies for that session and pull the right cookie, but that sounds like it'd be risky security-wise for a PHP server to allow (letting one session be aware of another). Example:
exec("wkhtmltopdf '$urlofmylots?sessionId=$sessionIdFromThisRequest' filename.pdf");
Possibly set a session variable and then pass that session Id, and see if I can use PHP to wade through that information instead (rather than using the cookie).
Would I be able to just create an array and somehow have that other script be aware of it, possibly by including it? That doesn't really solve the problem of wkhtmltopdf expecting a web-facing address as its first parameter.
(not really an idea, but some reasoning) In other instances of using this, I've just passed an ID to the script that generates the markup for wkhtmltopdf to parse, and the script uses that ID to get data from the database. I don't want to store this data in a file or the database for the simple purpose of transferring data from the caller to the callee in this case. Cookies and sessions seem cleaner since apache/php handle memory allocation for these sessions.
The ultimate problem here is that I'm trying to get my second script (referenced here by $urlofmylots) to be aware of data available to the calling script but it's being executed as if it were an external web request, not two php scripts being called from the web root.
Can anyone offer some insight here?
You might consider rendering whatever the output of $urlofmylots?lots=$lots_to_print would be to a temporary file and running wkhtmltopdf against that file.

Categories