Reputation: 83
I want to save data from C++ to PHP and here is what I have
string cmd = "wget localhost/tem1.php?t=1&date=1&time=1";
system((const char*) cmd.c_str());
as you can see I want to send parameter t, date and time, but it just sends t without the two others parameters, here is the output :
--2016-11-21 20:56:45-- http://localhost/tem1.php?t=1
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5 [text/html]
Saving to: 'tem1.php?t=1'
Why it only takes t ? please help me to solve this problem * Notice : When I write it in the link bar it will insert the data successfully Thank you
Upvotes: 1
Views: 35
Reputation: 74028
If this is on a Unix/Linux system, the reason is the ampersand &
. system
executes your command by executing a shell
The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows:
execl("/bin/sh", "sh", "-c", command, (char *) 0);
system() returns after the command has been completed.
The shell interprets &
as putting the command into the background. To prevent this, you must wrap the argument with quotes
string cmd = "wget \"localhost/tem1.php?t=" + to_string(temperature) + "&date=" + date + "&time=" + time + "\"";
This tells the shell to leave the ampersand alone.
Upvotes: 3