Go Back   SL Forums > Resident Forums > Content Creation > Scripting Tips
Welcome, Al Supercharge.
You last visited: 05-07-2008 at 06:33 AM
User CP GUIDELINES - please read before posting New Posts Quick Links Log Out


Reply
Thread Tools Search this Thread Display Modes
Unread 06-06-2008, 05:27 PM   #1
Nichiren Dinzeo
Registered User

Join Date: Feb 2007
Posts: 145
inserting data into db from SL question

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:

[php]
<?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());
?>
[/php]

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 is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 05:58 PM   #2
Nichiren Dinzeo
Registered User

Join Date: Feb 2007
Posts: 145
I found a few typos...but still no go. help!
Nichiren Dinzeo is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 06:16 PM   #3
Hewee Zetkin
Registered User

Join Date: Jul 2006
Posts: 1,327
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?
Hewee Zetkin is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 06:20 PM   #4
Haruki Watanabe
llSLCrash(void);

Join Date: Mar 2007
Posts: 315
First, I'd alter the PHP-Command for the insert like this:
[php]
mysql_query("INSERT INTO ".$usertable." (tb_name, tb_owner) VALUES('".$tbname."' , '".$tbowner."' ) ") or die(mysql_error());
[/php]
Then I'd alter the http-request in SL like this:

[php]

// 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);
}

}
[/php]

(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...
Haruki Watanabe is online now Report Bad Post   Reply With Quote
Unread 06-06-2008, 07:26 PM   #5
Nichiren Dinzeo
Registered User

Join Date: Feb 2007
Posts: 145
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;

[php]
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);
}

}
}
[/php]

And tbinsert.php on the server looks like this:

[php]
<?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());

?> [/php]


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.
Nichiren Dinzeo is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 07:33 PM   #6
Hewee Zetkin
Registered User

Join Date: Jul 2006
Posts: 1,327
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?
Hewee Zetkin is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 07:41 PM   #7
Nichiren Dinzeo
Registered User

Join Date: Feb 2007
Posts: 145
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

Quote:
Originally Posted by 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 is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 07:42 PM   #8
Nichiren Dinzeo
Registered User

Join Date: Feb 2007
Posts: 145
yes and it comes out blank. I wonder if this is becuase I am using joomla.


Quote:
Originally Posted by 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?
Nichiren Dinzeo is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 08:22 PM   #9
Haruki Watanabe
llSLCrash(void);

Join Date: Mar 2007
Posts: 315
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 «[url]http://www.mywebsite.com.com//myphp/tbinsert.php»[/url] 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 «[url]www.mywebsite.com/myphp/...»[/url]

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

[php]

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

[/php]
Haruki Watanabe is online now Report Bad Post   Reply With Quote
Unread 06-06-2008, 08:24 PM   #10
Nichiren Dinzeo
Registered User

Join Date: Feb 2007
Posts: 145
comes back blank but is see that it is updated the database ..with nothing of course. blank rows

Quote:
Originally Posted by Nichiren Dinzeo
yes and it comes out blank. I wonder if this is becuase I am using joomla.
Nichiren Dinzeo is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 08:25 PM   #11
Hewee Zetkin
Registered User

Join Date: Jul 2006
Posts: 1,327
Quote:
Originally Posted by 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.
Hewee Zetkin is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 08:32 PM   #12
Nichiren Dinzeo
Registered User

Join Date: Feb 2007
Posts: 145
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!



Quote:
Originally Posted by 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 «[url]http://www.mywebsite.com.com//myphp/tbinsert.php»[/url] 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 «[url]www.mywebsite.com/myphp/...»[/url]

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

[php]

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

[/php]
Nichiren Dinzeo is offline Report Bad Post   Reply With Quote
Unread 06-06-2008, 09:09 PM   #13
Haruki Watanabe
llSLCrash(void);

Join Date: Mar 2007
Posts: 315
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):

[php]

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());

[/php]

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?
Haruki Watanabe is online now Report Bad Post   Reply With Quote
Unread 06-07-2008, 02:20 AM   #14
Daten Thielt
Registered User

Join Date: Dec 2006
Posts: 84
replace your the $_POST['']; with $_REQUEST['']; my variables wouldl not carry over with pose i had to use request
Daten Thielt is offline Report Bad Post   Reply With Quote
Unread 06-07-2008, 08:50 AM   #15
Haruki Watanabe
llSLCrash(void);

Join Date: Mar 2007
Posts: 315
Quote:
Originally Posted by 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...
Haruki Watanabe is online now Report Bad Post   Reply With Quote
Reply



Posting Rules
You may post new threads
You may post replies
You may post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump



All times are GMT. The time now is 09:49 PM.


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright 2002-2007 Linden Lab