Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Automatically uploading info to a website?

Automatically uploading info to a website?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #15 on Wed 15 Aug 2018 10:43 PM (UTC)
Message
If so, it is because you rewrote my function urlencode and your version refers to a non-existent variable "str".

It might also be helpful to "assert" the socket.http.request so you get back some error message if it fails. Both changes here:



-- converts characters that are not alphanumeric
-- into the form %xx (for use in cookies, forms etc.)
function urlencode (s)
 return (string.gsub (s, "%W", 
        function (str)
          return string.format ("%%%02X", string.byte (str))
        end  ))
end -- function urlencode

function fixrequest(req)
	local t = {}
	for k, v in pairs (req) do
		table.insert(t, urlencode(k) .. '=' .. urlencode(v))
	end -- for
	return table.concat(t, "&")
end -- fixrequest 

function testUpload()
	http = require "socket.http" 
	request_body = fixrequest {character = "Aberdeen", qnum = 43, time = 1534372981}
	response_body = {}
	assert (socket.http.request {
    	url = "http://localhost/mm/abby.php",
		method = "POST",
		headers = {
			["Content-Length"] = string.len(request_body),
			["Content-Type"] =  "application/x-www-form-urlencoded"
		},   -- end of headers
		source = ltn12.source.string(request_body),
		sink = ltn12.sink.table(response_body)
	})  -- end of request table
	Note("done")
end --function testUpload


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #16 on Thu 16 Aug 2018 02:06 AM (UTC)

Amended on Thu 16 Aug 2018 02:11 AM (UTC) by Areadien

Message
No, I got my ColourNote saying "done," but no entries were inserted into my database. (cNote picks a random colour and then sets a cNote; I forgot to change that). And that's the case even with both of your changes.

Now, with the string.gsub, do I absolutely have to have the anonymous function? In other words, can I change the code to an equivalent that doesn't have it?
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #17 on Thu 16 Aug 2018 02:25 AM (UTC)
Message
Actually, I know why it didn't error. I had another function named urlencode after the one you provided, so it was using that one, I guess.

Also, I'm doing this now; it seems to be the equivalent, but I'm not sure.

function urlencode (s)
	return string.gsub (s, "%W", string.format ("%%%02X", string.byte ('%1')))
end -- function urlencode
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #18 on Thu 16 Aug 2018 02:26 AM (UTC)
Message
Nick Gammon said:

If so, it is because you rewrote my function urlencode


Yeah, I had to get rid of the anonymous function.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #19 on Thu 16 Aug 2018 05:35 AM (UTC)
Message
Areadien said:

Actually, I know why it didn't error. I had another function named urlencode after the one you provided, so it was using that one, I guess.

Also, I'm doing this now; it seems to be the equivalent, but I'm not sure.


It doesn't seem to work for me:


function urlencode (s)
	return string.gsub (s, "%W", string.format ("%%%02X", string.byte ('%1')))
end -- function urlencode

print (urlencode "jack goes to the market")


Give me:


Run-time error
World: smaug2
Immediate execution
[string "Immediate"]:2: invalid capture index
stack traceback:
        [C]: in function 'gsub'
        [string "Immediate"]:2: in function 'urlencode'
        [string "Immediate"]:5: in main chunk

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #20 on Thu 16 Aug 2018 05:36 AM (UTC)
Message
Areadien said:

Yeah, I had to get rid of the anonymous function.


You didn't have to, you chose to, and this improved function doesn't work quite as well.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #21 on Thu 16 Aug 2018 06:02 AM (UTC)

Amended on Thu 16 Aug 2018 06:04 AM (UTC) by Nick Gammon

Message
I had to tweak a few things to get it to work. The first test was to manually bring up the PHP page and get it to work. The amended PHP file is:


<?php
print_r($_POST);
if(!empty($_POST))
{
    $connect = @mysqli_connect('localhost', 'mm_i', 'password', 'mm');
    if($connect === false)
    {
        echo('<p>');
        echo('<span>Error: Unable to connect to MySQL on line 6 in mm/abby.php: ' . mysqli_connect_error() . '</span>');
        echo('</p>');
        echo('<p>');
        exit;
    }
    else
    {
        $query = "INSERT INTO myquests VALUES(NULL, '{$_POST['character']}', '{$_POST['qnum']}', '{$_POST['completed']}');";
        $result = mysqli_query($connect, $query)
            or die("Could not execute sql query <b>" . htmlentities($query) . "</b> in mm/abby.php: " . mysqli_error($connect));
        mysqli_close($connect) or die("Could not close server connection on line " . __LINE__ . " in mm/abby.php because: " . mysqli_error());
    }
}?>
<form action = "" method = "post">'
    <input name = "character" type = "text" value = "" /> <input name = "qnum" type = "text" value = "" /> <input name = "completed" type = "text" value = "" /><br />
    <input type = "submit" value = "Submit" />
</form>



Yours had various issues, for example "htmlentities($query)" where $query was not defined.

It also isn't great as HTML, you should really have <html><head><body> etc. tags in it so it looks like a proper web page. However for computer filling-in it is probably OK.

Once that was sorted, I could fill in the PHP page, press Submit, and a record was added to the database.

I also tweaked the script (for example the urlencode). You also had:


	request_body = fixrequest {character = "Aberdeen", qnum = 43, time = 1534372981}


However the PHP page called the variable "completed" not "time". You have to get details like this right.



-- converts characters that are not alphanumeric
-- into the form %xx (for use in cookies, forms etc.)
function urlencode (s)
 return (string.gsub (s, "%W", 
        function (str)
          return string.format ("%%%02X", string.byte (str))
        end  ))
end -- function urlencode

function fixrequest(req)
	local t = {}
	for k, v in pairs (req) do
		table.insert(t, urlencode(k) .. '=' .. urlencode(v))
	end -- for
	return table.concat(t, "&")
end -- fixrequest 

function testUpload()
	http = require "socket.http" 
	request_body = fixrequest {character = "Aberdeen", qnum = 43, completed = 1534372981}
	response_body = {}
	assert (socket.http.request {
    	url = "http://localhost/mm/abby.php",
		method = "POST",
		headers = {
			["Content-Length"] = string.len(request_body),
			["Content-Type"] =  "application/x-www-form-urlencoded"
		},   -- end of headers
		source = ltn12.source.string(request_body),
		sink = ltn12.sink.table(response_body)
	})  -- end of request table
	Note("done")
end --function testUpload


With all that done, it does indeed add records to the database.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #22 on Thu 16 Aug 2018 06:55 PM (UTC)
Message
Nick Gammon said:

Areadien said:

Yeah, I had to get rid of the anonymous function.


You didn't have to, you chose to, and this improved function doesn't work quite as well.


Yeah, that was an error. I meant, "I had to if I wanted to get rid of the anonymous function." Not that I actually had to get rid of the anonymous function.
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #23 on Thu 16 Aug 2018 06:58 PM (UTC)
Message
Nick Gammon said:

Yours had various issues, for example "htmlentities($query)" where $query was not defined.


Yeah, that's what I get for using my own functions--in this case, it's called sqlQuery($query)--and not thoroughly changing them so that I don't have to specifically provide the definitions for them.
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #24 on Fri 17 Aug 2018 03:03 AM (UTC)
Message
Oh, I forgot to mention that the code does indeed work, even on my end. Helped when I put your Lua into a different plugin than the one I was trying to use, since I had conflicting code in it.
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #25 on Mon 20 Aug 2018 05:30 PM (UTC)
Message
Is it possible to do this with $_GET? I've read that "GET" is the default for the method, but replacing "POST" and leaving the rest of the code as is doesn't work.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #26 on Mon 20 Aug 2018 08:44 PM (UTC)
Message
Why fiddle with it, if it is working? But yes, you can do a GET-type request.

If you look at reply #3 in this thread you will see that the GET parameters are in a different place (on the URL) so you would need to add them to the end of the URL, like:


url = "http://localhost/mm/abby.php?character=Aberdeen&qnum=43",


Then the request_body would be empty.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


60,083 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.