0

So I have a backend code where I echo out the results in my database as an array Jobject and also a code (that iam going to show first) where I try to make something happen when I want to create something on my DB via my frontend.

So my question is, How should I adjust the first part of the code so my frontend code can reach it and so that the info that the user puts in the frontcode gets into the database?

<?php 

$mysql_pekare= new mysqli ("host", " user","pass", "db");

$stmt = $mysql_pekare->prepare("INSERT INTO Events(`Name`, `Title`) VALUES(?,?)");

$stmt->bind_param("ss", $namn, $age);

$stmt->execute();

$stmt->close();

$mysql_pekare->close();

?>

And finally this is my frontend code (C#) where I try to reach the DB and my PHP-code and create something into my DB from my frontend:

static public async Task <bool>  createOurMainInfo (string userId, string thename, string thetitle)
    {
        var httpClientRequest = new HttpClient ();

        var postData = new Dictionary <string, string> ();

        postData.Add ("Name", thename);
        postData.Add ("Title", thetitle);

        var jsonRequest = JsonConvert.SerializeObject(postData);

        HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");

        var result = await httpClientRequest.PostAsync("http://localhost/Events.php", content);
        var resultString = await result.Content.ReadAsStringAsync ();

        return  true;
     }

Updated code:

$value1 = json_decode(file_get_contents('php://input'));
$value2 = json_decode(file_get_contents('php://input'));

$mysql_pekare= new mysqli ("", "","", "");

$stmt = $mysql_pekare->prepare("INSERT INTO Events(`Name`, `Title`) VALUES(?,?)");

$stmt->bind_param("ss", $value1, $value2);

$stmt->execute();

$stmt->close();

$mysql_pekare->close();

echo $value1->Name;
echo $value2->Title;
William.John
  • 127
  • 1
  • 9

1 Answers1

0

The c# code posts 2 parameters as Name and Title as json, so your 1st php code should expect 2 parameters named as Name and Title in json format and submit those 2 parameters to the insert query in the place of $namn and $age.

Since you are posting the data as json, you cannot use $_POST directly. php://input enables you to read raw post input and json_decode() can be used to decode the contents of a json string into objects.

$value = json_decode(file_get_contents('php://input'));
echo $value->Title; //should return title

Obviously, I would add some authentication and authorisation to the php code and some input checking on the json parameters before submitting them to the database.

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • $stmt->bind_param("ss", $_POST Name, $_POST Title); Would I add it directly like this? – William.John Mar 13 '16 at 23:57
  • 1
    Pls check the link to the manual I gave on $_post, but sg like that is needed. But pls try the code yourself. – Shadow Mar 14 '16 at 00:00
  • Yeah I meant like this: $stmt->bind_param("ss", $_POST ['Name'], $_POST ['Title']); . When I try to test it on my frontend it doesnt work and the info i entered wont save on the db – William.John Mar 14 '16 at 00:02
  • 1
    I'm sorry, but the "does not work" is a meaningless feedback! Debug your code to retrieve a proper error message or understand where things go south. There is still a space before [ in variable names. – Shadow Mar 14 '16 at 00:06
  • In the log when I create something in my frontend I recieve this: {"Name":"Test1","Title":"Testtt1"} so the info reaches the 2 parameters in the frontcode i assume atleast. I am not sure how else I can check what is wrong. – William.John Mar 14 '16 at 00:11
  • I also recieve the current info from the DB. So i recieve the json that alrdy exists on the database in my frontend log. – William.John Mar 14 '16 at 00:13
  • 1
    By adding debugging code to your php code, perhaps? You can echo php error messages, or save them to a file. You can also catch sql error messages from php and output them as well. – Shadow Mar 14 '16 at 00:15
  • Okok. I have not used that kind of code before. How could it look? – William.John Mar 14 '16 at 00:20
  • 1
    I'm sorry, but this is a Q/A site, not an introduction to php coding class. – Shadow Mar 14 '16 at 00:22
  • I am aware of that, but ofc I cant accept your answer as the issue still remains – William.John Mar 14 '16 at 00:23
  • 1
    Updated my answer. However, pls do your own debugging from now on. If you are unable to debug code and can only accept full copy-paste code, then SO may not be the right place for your questions! – Shadow Mar 14 '16 at 00:32
  • Updated my question with a new code (in the buttom). Am I working towards the right direction? – William.John Mar 14 '16 at 00:42
  • 1
    Not really. No need to access the raw input twice, only once. Pls make an effort to understand what is in $value. I linked the relevant php manual pages. The echos are for debugging purposes only. Have you checked their return value at all? – Shadow Mar 14 '16 at 00:49
  • Okok. Yeah trying to solve this in a rush as it is late here. Well gotta keep working on it tomorrow then. I will keep you updated if I make any progress with your tips tomorrow. Thx – William.John Mar 14 '16 at 00:52
  • 1
    One more hint then: json_decode() returns an object. That object holds all data within the json (name and title). – Shadow Mar 14 '16 at 00:58
  • $stmt->bind_param(array("ss", $value[0], $value[1])); Something like this? – William.John Mar 14 '16 at 15:15
  • I get this error: Wrong parameter count for mysqli_stmt::bind_param() – William.John Mar 14 '16 at 16:21
  • 1
    I'm sorry, but why are you guessing? 1. I gave you the sample code how to access the parameters. 2. You are still not debugging your own code. – Shadow Mar 14 '16 at 19:05
  • Yeah, I am just having trouble to get the object into two pieces so I can add it into my Values. How can I chop up json_decode() into two? – William.John Mar 14 '16 at 19:41
  • $stmt->bind_param("ss", $value->Name, $value->Title); Nothing adds into the DB with this code either. I am sorry for not reading to much into this. I am mostly into frontcode, so I just want to get this backend done so I can focus on the front mor ein the future. I just want to make this work so I can work with DB items in my front. – William.John Mar 15 '16 at 13:38
  • I would be very grateful if you helped me get this done. – William.John Mar 15 '16 at 14:51
  • I get nothing when I type this: $data = json_decode(file_get_contents('php://input'), true); print_r($data); and echo $data["Name"]; same with $data->Title; – William.John Mar 15 '16 at 23:50
  • when I use "encode" instead, i get 3 of these """ – William.John Mar 15 '16 at 23:54
  • the problem is that it doesnt find my c# inputted info. – William.John Mar 15 '16 at 23:59