Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

inserting data into db from SL question

Nichiren Dinzeo
Registered User
Join date: 14 Feb 2007
Posts: 203
06-06-2008 10:27
I have read a lot of posts on this topic but am still having troubles. hope one of you gurus can help.

table in server db looks like this:

tbid, tb_name, tb_owner

tbinsert.php on server looks like this:

CODE

<?php
$tbname = $_POST['tbname'];
$tbowner = $_POST['tbowner'];

//Connect To Database
$hostname='p3sxxxxxxx.secureserver.net';
$username='myusername';
$password='mypword';
$dbname='mydbname';
$usertable='mytable';


mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname); //tested this and its connected fine


mysql_query("INSERT INTO $usertable (tb_name, tb_owner) VALUES('$tbname' , '$tbowner' ) ") or die(mysql_error());
?>


script on SL object looks like this:





default
{
state_entry()
{
llSetTimerEvent(30.0);
llOwnerSay("Your Travel Bug is sending its info to a web database located at ";);
}

timer()
{

key ownerKey = llGetOwner();
string tbowner = llKey2Name(ownerKey);
string tbname = "mytbname";


//msg = llGetSubString(msg, 3, end);
llWhisper(0, "Updating mywesite.com with '"+tbowner+"'.";);
llHTTPRequest("http://www.mywebsite.com/myphp/tbinsert.php", [HTTP_METHOD, "POST"],
"?tbowner="+(string)tbowner+"&tbname="+(string)tbname);
}
}


Somethings not right. Thanks
Nichiren Dinzeo
Registered User
Join date: 14 Feb 2007
Posts: 203
06-06-2008 10:58
I found a few typos...but still no go. help!
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
06-06-2008 11:16
WHAT isn't right? Are you getting errors in your web server log? If so, posting them here might help so we have something to go on. Also, what about your 'tbid' column? Is it an auto-increment column, does it have a default, are any indexes/keys defined on it, etc.? For that matter, do you have any keys or indexes defined for the table at all?
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
06-06-2008 11:20
First, I'd alter the PHP-Command for the insert like this:
CODE

mysql_query("INSERT INTO ".$usertable." (tb_name, tb_owner) VALUES('".$tbname."' , '".$tbowner."' ) ") or die(mysql_error());

Then I'd alter the http-request in SL like this:

CODE


// define a global ke for the http-request:

key gMyRequest;

state_entry(){

gMyRequest = llHTTPRequest("http://www.mywebsite.com/myphp/tbinsert.php", [HTTP_METHOD, "POST"], "tbowner="+(string)tbowner+"&tbname="+(string)tbname);
}

// Call the http_response after calling llHTTPRequest

http_response(key id,integer status, list meta, string body)
{
if(bMyRequest==id)
{
llOwnerSay(body);
}

}


(No need for the ? at the beginning of the parameters)...

When you call http_response in SL you should see the answer from your PHP-script, means, if there's a MySQL-Error, you should get it back to SL...
Nichiren Dinzeo
Registered User
Join date: 14 Feb 2007
Posts: 203
06-06-2008 12:26
thanks for the input!! I made the changes you recommend.

SL script now looks like this (I added to the timer function i hope that is ok?):

key gMyRequest;

CODE

default
{
state_entry()
{
llSetTimerEvent(30.0);

}

timer()
{

key ownerKey = llGetOwner();
string tbowner = llKey2Name(ownerKey);
string tbname = "mytbname";


//msg = llGetSubString(msg, 3, end);
llWhisper(0, "Updating mywesite.com with '"+tbowner+"'.");
gMyRequest = llHTTPRequest("http://www.mywebsite.com.com//myphp/tbinsert.php", [HTTP_METHOD, "POST"], "tbowner="+(string)tbowner+"&tbname="+(string)tbname);
}
http_response(key id,integer status, list meta, string body)
{
if(gMyRequest==id)
{
llOwnerSay(body);
}

}
}


And tbinsert.php on the server looks like this:

CODE

<?php



//Connect To Database
$hostname='p3xxxxxxx.secureserver.net';
$username='myusername';
$password='mypasswrd';
$dbname='dbname';
$usertable='mytable';


mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);

mysql_query("INSERT INTO ".$usertable." (tb_name, tb_owner) VALUES('".$tbname."' , '".$tbowner."' ) ") or die(mysql_error());

?>



the SL output I see is something like this now (I had to do a screen capture and translate it here):

0#header.searchbox{
)floatleft;
0}
0#header.searchbox.q{
0width.300pcx;
)
0#header.wrap{
0float.left;
0margin.8pcx

0} etc.. it goes on with more header stuff but nothing significant.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
06-06-2008 12:33
That looks like a stylesheet or something. Maybe the CSS for the error page? Look at your the error log file for your web server. It can seriously be of great help.

Oh, and have you tried pointing your web browser at the URL directly?
Nichiren Dinzeo
Registered User
Join date: 14 Feb 2007
Posts: 203
06-06-2008 12:41
good questions...i am hosted by godaddy.com and am a newbie. I will see if there is a web server log avail to me.
`tb_id` INT( 11 ) NOT NULL DEFAULT '0' AUTO_INCREMENT PRIMARY KEY

From: Hewee Zetkin
WHAT isn't right? Are you getting errors in your web server log? If so, posting them here might help so we have something to go on. Also, what about your 'tbid' column? Is it an auto-increment column, does it have a default, are any indexes/keys defined on it, etc.? For that matter, do you have any keys or indexes defined for the table at all?
Nichiren Dinzeo
Registered User
Join date: 14 Feb 2007
Posts: 203
06-06-2008 12:42
yes and it comes out blank. I wonder if this is becuase I am using joomla.


From: Hewee Zetkin
That looks like a stylesheet or something. Maybe the CSS for the error page? Look at your the error log file for your web server. It can seriously be of great help.

Oh, and have you tried pointing your web browser at the URL directly?
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
06-06-2008 13:22
When you call a PHP-page that expects POST-vars without sending any form-vars, you usually won't see anything as an output. Your's should produce an error, though, since the POST-vars are missing...

I guess, this «http://www.mywebsite.com.com//myphp/tbinsert.php» is just an example, is it? Because «mywebsite.com.com//» will not work (maybe you have it in the original URL as well... double com, double slash... this example should read «www.mywebsite.com/myphp/...»

You might consider adding a check, whether the POST-vars are present in the PHP-file:

CODE


if(isset($_POST['tbname'])){
// do your stuff here
}else{
echo "No POST-vars present";
}

Nichiren Dinzeo
Registered User
Join date: 14 Feb 2007
Posts: 203
06-06-2008 13:24
comes back blank but is see that it is updated the database ..with nothing of course. blank rows

From: Nichiren Dinzeo
yes and it comes out blank. I wonder if this is becuase I am using joomla.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
06-06-2008 13:25
From: Haruki Watanabe
When you call a PHP-page that expects POST-vars without sending any form-vars, you usually won't see anything as an output. Your's should produce an error, though, since the POST-vars are missing...

Ah. True. Of course, you could just create a form on a second page that posts to the one we are testing.
Nichiren Dinzeo
Registered User
Join date: 14 Feb 2007
Posts: 203
06-06-2008 13:32
yes..just an example..and the // are typos.. the scripts are correct on my end. i will try as you suggest. I really do appreciate your help thanks!



From: Haruki Watanabe
When you call a PHP-page that expects POST-vars without sending any form-vars, you usually won't see anything as an output. Your's should produce an error, though, since the POST-vars are missing...

I guess, this «http://www.mywebsite.com.com//myphp/tbinsert.php» is just an example, is it? Because «mywebsite.com.com//» will not work (maybe you have it in the original URL as well... double com, double slash... this example should read «www.mywebsite.com/myphp/...»

You might consider adding a check, whether the POST-vars are present in the PHP-file:

CODE


if(isset($_POST['tbname'])){
// do your stuff here
}else{
echo "No POST-vars present";
}

Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
06-06-2008 14:09
Hmmm - I really wonder, whether your POST-vars make it to the script an whether the SQL-statement is right...

Try the following (in the PHP-File):

CODE


print_r($_POST); // This will output all postvars to the SL-script

// Then instead of issuing the mysql-command with the command built in do this:

$sql ="INSERT INTO ".$usertable." (tb_name, tb_owner) VALUES('".$tbname."' , '".$tbowner."' ) ";

echo "\nSQL-Statement: ".$sql;

mysql_query($sql) or die(mysql_error());



In Secondlife, this should call the http_request-event and there you should see a) all your Postvars and b) the sql-statement that results from them...

btw... I don't know why you made your http-request into a timer()-event? this would simply call the llHTTPRequest ever 30 Seconds with the same values... which is - uhm - not really useful? ;)
Daten Thielt
Registered User
Join date: 1 Dec 2006
Posts: 104
06-06-2008 19:20
replace your the $_POST['']; with $_REQUEST['']; my variables wouldl not carry over with pose i had to use request
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
06-07-2008 01:50
From: Daten Thielt
replace your the $_POST['']; with $_REQUEST['']; my variables wouldl not carry over with pose i had to use request


I would not recommend that, unless, you have a very odd installation of PHP or an ancient-version installed. The global arrays $_POST and $_GET should be used whenever possible;

If the variables aren't submitted, you most likely forgot to add the correct mime-type for the HTTP-request.

llHTTPRequest("http://www.my_server.com/my_script.php",[HTTP_METHOD,"POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], "var1=test&var2=test2";);

Sorry - totally forgot about that one - I'm pretty sure this is causing the problem...
Nichiren Dinzeo
Registered User
Join date: 14 Feb 2007
Posts: 203
06-10-2008 10:59
UGGG! found the issue after re-reading all your helpful posts and noticed that on one of my posts I show my website as:
://www.mywebsite.com.com

dbl checked my script and yup..I had two .coms on there. Its working now. Very very sorry for the pain. You all are great!