Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Why is llHttpRequest not writing data into my db?

Tomfox Wiranata
Registered User
Join date: 20 Dec 2006
Posts: 80
01-07-2007 18:17
Hi everyone...

I will make it short. Can u get from my code, why no data is written into my sql-database?


PHP CODE:
CODE

<?php







// Database Settings
define('DB_HOST','*****'); // MySQL Server Host
define('DB_USER','****'); // MySQL User
define('DB_PASS','******'); // MySQL Password
define('DB_NAME','*******'); //The name of the database

//Connect to the database or throw a nasty error
$db = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME,$db) or die(mysql_error());



switch(strtoupper($_REQUEST['command'])){
case 'STORE':
cmd_store($_REQUEST);
break;
default:
echo ("Invalid Command Requested");
}

//close the database connection
mysql_close($db);

//stop execution
die();


function cmd_store($params){
// Check for required parameters
$req = array('slname','slid','sllocation','sl_n1','sl_n2');
foreach($req as $rkey){
$$rkey = mysql_real_escape_string($params[$rkey]);
}

//create and execute the query
$sql = "INSERT INTO lslstore (name, id, location, num1, num2) VALUES ('$name', '$id', '$location', '$num1', '$num2', NOW())";
$result = mysql_query($sql) or die(mysql_error());
echo 'Store successful.';
}



}
?>



CODE

key requestOfServerStatusRequestid;
key saveData;

string serverStatus="no_status_received";


string name;
key currentUser;
string location;
string n1;string n2;string n3;string n4;string n5;string n6;

default
{
state_entry()
{



}


touch_start(integer number)
{

name = llDetectedName(0);
currentUser = llDetectedKey(0);
location = "Home";
n1="1";
n2="2";


string urlStore = "http://xxx.xxx.com/datei.php?command=store&slname="+name+"&slid="
+(string)currentUser+"&sllocation="+location+"&sl_n1="+n1+"&sl_n2="+n2;

saveData = llHTTPRequest(urlStore,[HTTP_METHOD,"GET"],"");




}


http_response(key request_id, integer status, list metadata, string body)
{
if (request_id == saveData)
{
// The response to save of status should be "Store successful."
llWhisper(0, "Response to store attempt: " + body);
}
else if (request_id == requestOfServerStatusRequestid)
{
// The response to server status request is the server status
llWhisper(0, "Response to server status request: " + body);
serverStatus = body;

}
else
{
llSay(0,(string)status+" error");
}
}





}


login information for my db are correct. tried it with php-scripts. thx so much
Adriana Caligari
Registered User
Join date: 21 Apr 2005
Posts: 458
01-08-2007 01:20
Adding a commit after the insert might help

Also add a mail call in the routine so that you can see where it is going.
_____________________
Maker of quality Gadgets
Caligari Designs Store
Tomfox Wiranata
Registered User
Join date: 20 Dec 2006
Posts: 80
01-08-2007 02:58
Hello Adriana. Thx for your help. Did not see that at 3:15 am...

i changed the code as follows:
CODE

<?php

// Database Settings
define('DB_HOST','xxxxxx'); // MySQL Server Host
define('DB_USER','xxxxx'); // MySQL User
define('DB_PASS','xxxxxx'); // MySQL Password
define('DB_NAME','xxxxxx'); //The name of the database

//Connect to the database or throw a nasty error
$db = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME,$db) or die(mysql_error());


switch(strtoupper($_REQUEST['command'])){
case 'STORE':
cmd_store($_REQUEST);
break;
default:
echo ("Invalid Command Requested");
}

//close the database connection
mysql_close($db);

//stop execution
die();


* Command: STORE
* Usage: Stores a string in the database

**/
function cmd_store($params){
// Check for required parameters
$req = array('slname','slid','sllocation','sl_n1','sl_n2');
foreach($req as $rkey){
$$rkey = mysql_real_escape_string($params[$rkey]);
}
//$owner_id = get_owner_id();
//create and execute the query
$sql = "INSERT INTO lslstore (name, id, location, num1, num2) VALUES ('$name', '$id', '$location', '$num1', '$num2')";

$result = mysql_query($sql) or die(mysql_error());
echo 'Store successful.';
}
?>


added the commit etc. now sl says "store successful". and indeed a new line is written in my database, BUT WITHOUT data. just see empty cells...

at least a little step forward....thx so much

PS: lsl-code is still the same
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
01-08-2007 04:09
I dont know any php but do string parameters have to be enclosed in double quotes?
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
01-08-2007 05:32
insert your variables proplery:

$sql = "INSERT INTO lslstore (name, id, location, num1, num2) VALUES ('".$name."', '".$id."', '".$location."', '".$num1."', '".$num2."')";

the easier fashion does not always work.

you will also want to echo the variable contents to make sure they have anything in them

and maybe check the $result
_____________________
Dimentox Travanti
DCS Coder
Join date: 10 Sep 2006
Posts: 228
01-08-2007 06:31
Need doubble quotes.


example
CODE

<?
$test = "Blahhhhhh";
echo '$test';
echo "\n";
echo "$test";
?>


OUTPUT
CODE

php blahh.php
Content-type: text/html
X-Powered-By: PHP/4.3.9

$test
Blahhhhhh
_____________________
LSL Scripting Database - http://lsl.dimentox.com
Mintopia Ambassador
Registered User
Join date: 18 Mar 2005
Posts: 4
01-08-2007 07:49
The reason it's not working is, you're assigning the request variables in the foreach loop, but you're using different variable names in the MySQL query. eg. $slname contains the name, but you're using $name in the query.

My version of the code with a few changes (I've enclosed the variables in the query in {}, added backticks to the query and removed the unneeded mysql_close() and die(). You also don't need to specify the MySQL connection in mysql_select_db().

CODE

<?php
// Database Settings
define('DB_HOST','xxxxxx'); // MySQL Server Host
define('DB_USER','xxxxx'); // MySQL User
define('DB_PASS','xxxxxx'); // MySQL Password
define('DB_NAME','xxxxxx'); //The name of the database

//Connect to the database or throw a nasty error
$db = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());

switch(strtoupper($_REQUEST['command'])){
case 'STORE':
cmd_store($_REQUEST);
break;
default:
echo "Invalid Command Requested";
}

/*
* Command: STORE
* Usage: Stores a string in the database

*/

function cmd_store($params){
// Check for required parameters
$req = array('slname','slid','sllocation','sl_n1','sl_n2');
foreach($req as $rkey){
$$rkey = mysql_real_escape_string($params[$rkey]);
}
$owner_id = get_owner_id();
//create and execute the query
$sql = "INSERT INTO `lslstore` (`name`, `id`, `location`, `num1`, `num2`) VALUES ('{$slname}', '{$slid}', '{$sllocation}', '{$sl_n1}', '{$sl_n2}')";

mysql_query($sql) or die(mysql_error());
echo "Store successful.";
}
?>
Tomfox Wiranata
Registered User
Join date: 20 Dec 2006
Posts: 80
01-08-2007 13:45
thx mintopia...it worked....and thx to all of you ;)