PDA

View Full Version : Someone help me... I have problem with sending data to my mysql table



Berone
July 10th, 2004, 10:44 PM
<?
session_start();
$conn = mysql_connect("XXX", "XXX", "XXX");
mysql_select_db("XXX",$conn);



if ($_POST['score']) {
$score = $_POST['score'];
}
print "status=Finished";

$sql = "select * from XXX where user_name = 'XXX'";
$sql_res = mysql_query($sql,$conn) or die(mysql_error());
srand ((float) microtime() * 10000000);
while ($stats = mysql_fetch_array($sql_res)) {
$gold = $stats['cash'];


}

$gold = $gold + $score;


$q = "update usergame set cash = '$gold' where user_name = '$HTTP_SESSION_VARS[user_name]'";
mysql_query($q);

?>

$gold outputs as 0
$score which is the score from the flash game works!
cash is updated to the same as score... because $gold doesn't register!
help i donno why this does htis

Berone
July 10th, 2004, 11:15 PM
I think the problem is that when i try and select data ... it doesnt work because i tried to select something else and it didn't work...
ugh... this is driving me nuts

ahmed
July 11th, 2004, 12:03 AM
This doesn't belong in Flash ActionScript so I moved it over.


When $_POST['score'] isn't set, is the script still supposed to run?

If you're just trying to add score to the gold, you can always go

"UPDATE mytable SET cash = cash + score WHERE... etc"

hope this helps

Berone
July 11th, 2004, 12:39 AM
$q = "update usergame set cash = cash + $score where user_name = '$HTTP_SESSION_VARS[user_name]'";
mysql_query($q);

i used that and it still doesn't add cash to score to get cash...
it just inserts score

if have if (!$_POST['score']) {
do this
} else {
$score = $_POST['score'];

etc...

i donno why cash doesn't register... score should be the one that doesn't

Berone
July 11th, 2004, 12:41 AM
i put if (!$gold) {

before to see if gold even got selected and it didnt

ol4pr0
July 11th, 2004, 12:54 AM
$result = mysql_query($q) or die(mysql_error());
#this is for debuggin of queries which should really
#add to all queries when still
#underconstruction
echo $result;

Berone
July 11th, 2004, 12:57 AM
Its updating fine the process is that cash isn't getting selected and gold isn't getting defined

ol4pr0
July 11th, 2004, 01:08 AM
#try this.
$sql = "select * from XXX where user_name = 'XXX'";
$sql_res = mysql_query($sql,$conn) or die(mysql_error());
srand ((float) microtime() * 10000000);
while ($stats = mysql_fetch_array($sql_res)) {

$gold = stats['cash'] + $score;
echo $gold; #to check if its all oke.
$q = "update usergame set cash ='".$gold."' where user_name ='".$HTTP_SESSION_VARS[user_name]."'";
$result = mysql_query($q) or die(mysql_error());
echo $result; #debug whats output..
}

Berone
July 11th, 2004, 01:15 AM
ok
i found the error Resource id #2
when itry and select cash
i donno what the means or how to fix it

Berone
July 11th, 2004, 01:21 AM
$gold = 406 $result = status=1
$gold shouldn't of been 406 becuase 406 is what i scored....
it should of added 100 to 406
because 100 was my initial gold but i tdidnt select it right

Berone
July 11th, 2004, 01:22 AM
oh i gues that status = 1 should just be 1 so yea it updated fine

ol4pr0
July 11th, 2004, 01:24 AM
As you can see now, echoing back youre variables for debug is a very good thing to do :) it will solve alot of youre problems while coding the pages. the mysql_error is the same thing. And if the query really doesnt work try it directly from the prompt command or in any other query window. that to will make you see youre errors very fast.

Berone
July 11th, 2004, 01:26 AM
lol yea it does help but my problem still remains! i can't get cash selected

Berone
July 11th, 2004, 01:28 AM
Resource id #2 thats the error message i get when i try and select stufff

ol4pr0
July 11th, 2004, 01:29 AM
Well as i said execute that query in a prompt window or www.mysqlfront.de use that t00l thats what i use, it helps me with a lot of things.. or just phpmyadmin.. make the query in the query window and see if you get results. and again echo back all vars that you needs to see where it went wrong!

Berone
July 11th, 2004, 01:30 AM
Resource id #2 do u kno what that means though?

ol4pr0
July 11th, 2004, 01:32 AM
#this into
$sql_res = mysql_query($sql,$conn) or die(mysql_error());
#this
$sql_res = mysql_query($sql) or die(mysql_error());

ol4pr0
July 11th, 2004, 01:33 AM
It means something wrong with the query :) Thats why you should try them out in a query window instead from php pages. it really helps!

Berone
July 11th, 2004, 01:36 AM
Resource id #2
i still get it lol

Berone
July 11th, 2004, 01:47 AM
the thing updates fine it just doesn't select cash

ol4pr0
July 11th, 2004, 01:49 AM
Look. echo back all the vars you need, where ever a variable is not being returned thats where you need to look. And try them from a query window !

also try them directly without using a $var example;



# dont put in $username, but something like administrator if thats the user_name
$sql = "select * from users where user_name=administrator";

#and
echo stats['cash']; #does this return a echo ?

Berone
July 11th, 2004, 01:54 AM
kk i did that and stil no go
it still got gold as 0

Berone
July 11th, 2004, 01:55 AM
this works when i go to it but it doesn't work when i use it with my flash game

Berone
July 11th, 2004, 01:56 AM
when i don't use it with the flash game i get that same Resource #id=2 error but the gold outputs correctly

ol4pr0
July 11th, 2004, 01:59 AM
so if echo $gold; =0 ? it messes things up there right ? did you do a google ?

Berone
July 11th, 2004, 02:03 AM
yea gold should actually = something over 0 cuz its set at like 100 right now i think

lol no i haven' done a google but i did google looking for that resource #id thinger couldn't find anything lol u try u sound like a pro googler

ol4pr0
July 11th, 2004, 02:08 AM
$gold = '11';
$cash = '2';
$gold = $gold + $cash;
echo $gold;
#this will give a output of 13 so nothing wrong with youre code there.
#that means you need to check something here if $gold = 0
echo 'Score =';
echo $_POST['score'];
echo '<br>Cash = ';
echo $stats['cash'];
$gold = $_POST['score'] + $stats['cash'];
echo 'Total gold = ';
echo $gold;

So one of them should be empty which is it ? and what is youre query compleet, personally i do not care about youre table name neither about the variable you use for username however it might help.

And did you try the query in a query window instead using the php page ????

google as php.net are good resources :)

Berone
July 11th, 2004, 02:12 AM
i tried that where i est $gold as a certain thing and it worked it added gold to score right ... the prob is that the column cash isn't getting defined as gold and its drinvg me nuts

ol4pr0
July 11th, 2004, 02:18 AM
so you mean $stats['cash'] has not the right value ?
but it is updating correcly with the right values ????

Am i missing something here?

How is it getting defined, and how should it be ??? and what is the code belonging to that part ( not the whole thing/script. .. )

and what was the output of the small echo ing script above, what is it doing and what should it be ?

ps:
php.net


If you use mysql_connect with identical parameters twice (e.g. a second connection to log all sql queries without an impact on the first one), the resource variables will be different (echo $dbl says Resource id #1, echo $dbl_log says Resource id #2), but in some way the same (mysql_insert_id($dbl) and mysql_insert_id($dbl_log) are always identical).

Berone
July 11th, 2004, 02:43 AM
the $stats['cash']; has a value of 0 when it should have a value of like somethjing above 0... and i get the resource 2 erorr but i donno what that means exacstly

ol4pr0
July 11th, 2004, 02:46 AM
did you try the query window ??? AS i already stated out a couple of time...
again resource ids. basicly mean ( no mather which number it has, that you screwed up in the query. )

download this tool. www.mysqlfront.de (mysql front ) install it... than connect to youre database and run this query -> select cash from users where user_name='administrator' <-(example) query does that comes up with any result ?

Berone
July 11th, 2004, 02:52 AM
i did that and it worked i donno whats up

Berone
July 11th, 2004, 02:57 AM
$asdf = "select cash from usergame where user_name = '$HTTP_SESSION_VARS[user_name]'" or die(mysql_error());
$asdf_res = mysql_query($asdf) or die(mysql_error());
srand ((float) microtime() * 10000000);
while ($cats = mysql_fetch_array($asdf_res)) {

its doing everything in here so i know its found the data


i changed it to that... i still get that resource erro and it doesn't work

ol4pr0
July 11th, 2004, 03:02 AM
what is the output of ..


echo $HTTP_SESSION_VARS['user_name'];

Before running the query..
and this how i like to do my queries.


#this only for query window !
#select cash from usergame where user_name
#did you actually do that www.mysql-front.de again.

$asdf="SELECT * FROM usergame WHERE user_name='".$HTTP_SESSION_VARS['user_name']."'";
$asdf_res = mysql_query($asdf) or die(mysql_error());

Berone
July 11th, 2004, 03:10 AM
it didnt work lol

ol4pr0
July 11th, 2004, 03:13 AM
well there you go, like i said a long long long long time ago.. echo youre $vars that you need.
since echo $HTTP_SESSION_VARS['user_name']; didnt work or came up emtpy the problem is somewhere else, like as in where did you said the SESSION VAR ?

do this for a minute


print_r($_SESSION); #whats the output.. copy and paste it please


whats ure php build ?

Berone
July 11th, 2004, 03:17 AM
oh no the session var printed.... everything works fine i donno what the big prob is

ol4pr0
July 11th, 2004, 03:24 AM
#whats the output.. copy and paste it please
And if that as you said is not null, than you did not complete youre query in a q window as i asked you, because that measn that the query should get executed without RCid and than the $stats['cash'] shouldnt be NULL.



#other way to fetch array
while($result = mysql_fetch_assoc($query)) {
echo $stats['cash'];

You are messing up somwhere, however if you are not able to post the things that are asked, (giving back the outputs for example... ))helping you is impossible.!

ol4pr0
July 11th, 2004, 03:27 AM
And


#if that indeed give result meaning user_name was there in the print_r($_SESSION);
#do this...
$asdf="SELECT * FROM usergame WHERE user_name='".$_SESSION['user_name']."'";

Hans Kilian
July 11th, 2004, 07:44 AM
It seems wierd that you use where user_name = 'XXX' when you do the select and where user_name = '$HTTP_SESSION_VARS[user_name]' when you update. They should be the same.

It's also a bit wierd that you loop around fetching the rows you've selected. Shouldn't there only be one? If there are more, the amount of gold is set to the last row you fetch and you ignore the rest.

Also in your select you have from XXX. That should be from usergame.