Hello unreal engineers,
I tried to make a simple console program which sends a HTTP POST request to a php page. I’m sending a username to the php page, and the php should save the username to a database. But somehow whenever start the program, it connects with the php page, the php saves something to the database, but it’s empty, a null string.
Here is the code of C++:
#include <iostream>
#include <stdio.h>
#include <iostream>
#include <string>
#include "curl\include\curl\curl.h"
using namespace std;
void SendData(string &postData)
{
CURL *pCurl;
CURLcode res;
pCurl = curl_easy_init();
if (pCurl)
{
//Set target URL
curl_easy_setopt(pCurl, CURLOPT_URL, "http://127.0.0.1/master.php");
curl_easy_setopt(pCurl, CURLOPT_POST, 1);
//POST data
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, postData);
//Perform request
res = curl_easy_perform(pCurl);
//Check for errors
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s
",
curl_easy_strerror(res));
}
//Always cleanup
curl_easy_cleanup(pCurl);
}
curl_global_cleanup();
}
int main(void)
{
cout << "MySQL communicator v1.0
";
string post = "user=kolio";
SendData(post);
cout << "Executed
";
system("PAUSE");
return 0;
}
And this is the php script:
<?php
$user = null;
$id = null;
$user = $_POST'user'];
$conn = mysql_connect("localhost", "tts_user", "123");
if ($conn) {
$selDB = mysql_select_db("tts_db");
if ($selDB) {
//$user = "petko";
//$sql = "SELECT id FROM test WHERE user = \"" . $user . "\"";
$sql = "INSERT INTO test (user) VALUES('{$user}')";
$result = mysql_query($sql);
echo mysql_error($conn);
/*if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result))
{
$id = $row'id'];
}
}*/
}
}
?>
It seems like c++ doesn’t send anything, or I’m getting the POST vars wrong. Whenever I start the program, it returns me “undefined index user at line 24”. But the php executed again and saved a null string to the database. What I’m doing wrong?
PS. I’m aware of the safeties to check if POST is set and stuff like that.