🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Posting data to a web form using C++ and PHP

Started by
1 comment, last by vilar 3 years, 2 months ago

The C++ part of the program will connect to the php and add a new row, with empty data instead of values.

Any help in pointing out where I am going wrong is most appreciated

<

<?php

error_reporting(E_ALL);

//connect

$dbh=mysql_connect("localhost","root","");

//select database

mysql_select_db("onlinedata",$dbh);

//add new user

$first=$_POST["firstname"];

$last=$_POST["lastname"];

$email=$_POST["email"];

mysql_query("INSERT INTO userinfo VALUES(NULL,\"".$first."\",\"".$last."\",\"".$email."\")",$dbh);

mysql_close($dbh);

?>

the C++ Code is as follows:

#include <string>

#include <windows.h>

#include <WinInet.h>

#include <fstream>

#include <iostream>

#include <tchar.h>

using namespace std;

int main()

{

HINTERNET IN hOpen; // Handle from InternetOpen()

hOpen = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

CHAR *szUrl="http://127.0.0.1/output.php"; // Full URL

DWORD dwSize;

CHAR szHead[] = "Accept: */*\r\n\r\n";

// CHAR * szHead="Content-Type: application/x-www-form-urlencoded";

VOID * szTemp[25];

HINTERNET hConnect;

FILE * pFile;

if ( !(hConnect = InternetOpenUrl ( hOpen, szUrl, szHead,lstrlen (szHead), INTERNET_FLAG_DONT_CACHE, 0)))

{

cout << "Error !" << endl;

return 0;

} cout<<GetLastError()<<endl;

static TCHAR hdrs[] = _T("Content-Type: application/x-www-form-urlencoded");

static TCHAR frmdata[] = _T("firstname=John&lastname=Doe&email=charanders4@aol.com");

HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",_T(szUrl), NULL, NULL, NULL, 0, 1);

cout<<GetLastError()<<endl;

HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));

cout<<GetLastError()<<endl;

}

>

Advertisement

Deek880 said:
Any help in pointing out where I am going wrong is most appreciated

Imagine going to a doctor and when he attends to you, you do a little dance and go ouch. Then you ask the doctor “what am I doing wrong?" without giving the doctor any information at all.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

fleabay said:
what am I doing wrong?

-lol- them shoes slide to the left a lil' bit too much, usually when the beat reaches the fourth bar;

but in all fairness the OP has stated that:

Deek880 said:
C++ part of the program will connect to the php and add a new row, with empty data instead of values.

// add something like this:
first=_POST["firstname"];
echo "My first name is: ".$firstname

if the firstname is not echoed then make sure:

  • your php server is running (for example see if you can access http://127.0.0.1/phpinfo.php​ from your web browser), if you are following a tutorial, usually that's what they advise… if not you can also try and call it in your php code:
<?php phpinfo( ); ?>
  • you check ALL your return code values:
BOOL ret = HTTPSendRequest( ... ) // Returns TRUE if successful, or FALSE otherwise

cout<<GetLastError()<<endl; //  what does this print out on the console ?

if you still have problems, try the C++ part but without PHP like this:

https://stackoverflow.com/questions/10106816/how-to-contruct-httpsendrequest-method-of-wininet

and if u still have a problem then… maybe u should stick to game code and not email app code -lol-

yep ?

This topic is closed to new replies.

Advertisement