user1011394
user1011394

Reputation: 1666

POST JSON data to PHP via C# and store values in MYSQL

My Class in C# which should be send to PHP:

MyData data = new MyData();
data.Name = "MyName";
data.Type = "MyType";
data.ID = 1;
data.Description = "MyLongDescription...";

How do I send it to PHP via C# in JSON format? And how do I retrieve it in PHP? The next step should be to insert the submitted data/values into a MySQL DB.

C# -> Send some JSON Data to PHP -> Write JSON Data to MYSQL DB via PHP

Sample Database: MyData(varchar Name, varchar Type, int ID, varchar Description)

My current PHP Code:

$json = json_decode($input,true);

$connection = mysql_connect("localhost","root","");
if (!$connection)
{
     die(mysql_error());
}

mysql_select_db("MyDataDB", $con);

mysql_query("INSERT INTO MyData (Name, Type, OtherID, Description) 
VALUES('$json[Name]','$json[Type]','$json[ID]','$json[Description]')");

Any ideas?

Thanks for your efforts.

Upvotes: 0

Views: 2437

Answers (2)

Thinking Sites
Thinking Sites

Reputation: 3542

I'll answer this, but to Quentin's point, you've got a lot going on.

There is no direct interop between C# and PHP so you'd need to expose a web service from PHP and consume it in your C#. Better yet, have the C# save directly to mysql and don't worry about interop.

Upvotes: 0

Leri
Leri

Reputation: 12525

C# code to encode:

var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = jsonSerializer.Serialize(yourCustomObject);

PHP code to decode:

$decoded = json_decode($received_json_string);

What have you tried, by the way?

Upvotes: 2

Related Questions