punttisali Posted October 17, 2013 Share Posted October 17, 2013 (edited) I'm working on a small project in which I'm trying to send some values from my arduino microcontroller to my mysql database.. In my database I have a table witch only two columns called "column1" and "column2". I'm a beginner in php programming and I know this might be a really easy question to anyone who knows php better. I have a working code in php which can send values to my mysql table and it works. My problmem is really that I don't know how to read the string sent to my server so that I could separate the values included. My string looks like this: " yourdata1=123&yourdata2=456" (123 and 456 being examples). Here is my php code, insert_mysql.php: <?php foreach ($_REQUEST as $key => $value) { if ($key == "yourdata1") { $yourdata1 = $value; } if ($key == "yourdata2") { $yourdata2 = $value; } } // EDIT: Your mysql database account information $username = "xxxxxxxxx"; $password = "xxxxxxxx"; $database = "axxxxxxxt"; $tablename = "xxxxxxxx"; $localhost = "xxxxxxx.com"; // Check Connection to Database if (mysql_connect($localhost, $username, $password)) { @mysql_select_db($database) or die ("Unable to select database"); $query = "INSERT INTO $tablename (column1, column2) VALUES ($yourdata1,$yourdata2)"; $result = mysql_query($query); } else { echo('Unable to connect to database.'); } ?> So I would like to read the value of "yourdata1" to variable $yourdata1 and the value of "yourdata2" (after the &-sign in the string) to variable $yourdata2. I know, that the $query-line works just fine. The problem is in the reading part. Is foreach even a gooda way to do it? I would really appreciate your help, because I'm really stuck with this. It is also possible for me to send the data to the server in different forms if it makes this problem any easier. For example I could send "yourdata1 123 yourdata2 456". Edited October 17, 2013 by punttisali Quote Link to comment Share on other sites More sharing options...
vinny42 Posted October 17, 2013 Share Posted October 17, 2013 My string looks like this: " yourdata1=123&yourdata2=456" (123 and 456 being examples). That looks like URL data, which you can parse with parse_str(). See: http://www.php.net/manual/en/function.parse-str.php Quote Link to comment Share on other sites More sharing options...
punttisali Posted October 17, 2013 Author Share Posted October 17, 2013 Thank you for your quick response! I can't tell if it is URL data or not. Actually this is everything I'm sending with client.print from my microcontroller: POST /insert_mysql.php HTTP/1.1 Host: www.pxxxxxxx.com User-Agent: Arduino/1.0 Connection: close Content-Type: application/x-www-form-urlencoded; Content-Length: 12 yourdata=777&yourdata2=888 The last line is what I would like to read into my database. Is parse_str() the best way to do this? And I really would appreciate some detailed help for the reading and storing into variables, because my knowledge in this coding language is so poor :/ Quote Link to comment Share on other sites More sharing options...
vinny42 Posted October 17, 2013 Share Posted October 17, 2013 Ah, so you are sending a proper HTTP request through some network device? Or are you sending the data through some serial cable? The string you posted in your first post can be parsed with parse_str(), no problem. But you may want to look at $_POST too, perhaps PHP is already picking it up properly. Quote Link to comment 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.