Jump to content

php to mysql problem


punttisali

Recommended Posts

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 by punttisali
Link to comment
Share on other sites

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 :/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.