Page 1 of 2

[SOLVED] check one row one column, insert int into same row

Posted: Tue Jul 13, 2004 11:06 pm
by fresh
hey,


trying to work out this points sytem, and can't seem to think straight tonight, I guess, because I just can't find the logic in my codes, maybe you can help??

Code: Select all

<?php
require('dbconnect.php');
require('check_login');

$pts = 100;
$pass = $_GET['pass'];
if($pass != "test") {

include("wrong.htm");

} else {

@mysql_query("INSERT ".$pts." FROM users WHERE points && WHERE username == ".$_SESSION['username'].") or die(mysql_error());  


include("100.htm");

}
?>

alright, I simplified what I'm trying to do, really what Im asking now is, is this script above going to work?? I don't want to try unless I know it will...

so what Im trying to do here, if you can't tell by the script, is check the pass a user enters, and if correct that users session var will tell the server to go to that users row, and to scan over and add the 100 points to that users points row, also, Id like to be able to check the points row, after the point add, and if that number is over lets say 1000, it would update that same users rank row to something else... can someone please help me out with this, Im horrible at sql, ... man it's confusing... 8O

thanks in advance ;)

Posted: Wed Jul 14, 2004 12:54 am
by Weirdan

Code: Select all

update users set points=points+$points where username = '$_SESSION&#1111;username]'
?

not working...

Posted: Wed Jul 14, 2004 1:19 am
by fresh
says there is a parse problem on line 12

Code: Select all

<?php
require('dbconnect.php');
require('check_login.php');
$pts = 100;
$pass = $_GET['pass'];
if($pass != "test") {

include("wrong.htm");

} else {

@mysql_query("update users set points=points+$pts where username = '$_SESSION[username]'") or die(mysql_error());


include("100.htm");

}
?>
the mysql_query is line 12, how do you write that in the msql_query paretheses??? I dont know if you can add avriables in an sql string... if you can, how, thanks ;)

Posted: Wed Jul 14, 2004 1:27 am
by Weirdan

Code: Select all

$ php -l am.php
No syntax errors detected in am.php
Please post exact error message you've got.

my error

Posted: Wed Jul 14, 2004 1:40 am
by fresh
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'where username = ''' at line 1

Posted: Wed Jul 14, 2004 2:13 am
by Weirdan
that's it: $_SESSION['username'] is undefined or empty

well

Posted: Wed Jul 14, 2004 2:42 am
by fresh
I logged in and tried it then, and it didnt throw up any errors, but, it's not writting to the DB... is my script correct, it doesnt seem right to me for some reason.. thanks ;)



here's my php script:

Code: Select all

<?php
require('dbconnect.php');
require('check_login.php');
$pts = 100;
$pass = $_GET['pass'];
if($pass != "test") {
} else {
@mysql_query("update users set points='points+".$pts."' where username='".$_POST['uname']."'") or die(mysql_error());
}
?>


here's my form:

Code: Select all

&lt;form action='http://blah.com/blah/test.php'&gt;
username:&lt;input type='text' name='uname'&gt;
password:&lt;input type='text' name='pass'&gt;
&lt;input type='submit' value='go'&gt;
&lt;/form&gt;
I even tried passing the variable to the php script in hopes the sql query would pass that to the db the value of 100 the points variable '$pts'...

any reason why??? all help is appreciated thanks in advance ;)

Posted: Wed Jul 14, 2004 2:54 am
by Weirdan
then you need to take all the usual steps of debugging SQL query:
  • echo it before passing it to mysql_query
  • execute it in mysql client
  • check if there's corresponding row to update (execute the select with that WHERE clause)
  • and so on...

well

Posted: Wed Jul 14, 2004 2:58 am
by fresh
none of thats going to work, if I echo the two vars I have it's going to give there values, I know what they are, and I know thats right, what isnt right is the sql query thing, or are you saying it is, and you dont know whats wrong??? thanks :)

Posted: Wed Jul 14, 2004 3:02 am
by feyd
he was talking about echoing your sql query, just before you execute said query..

ohh sorry

Posted: Wed Jul 14, 2004 3:08 am
by fresh
yeah that would be smart to do huh .. :oops:

anyway, fixed it :D , but it still says this:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '+='100' where username='test'' at line 1

see how it gets the var vals now, woot!!!, so whats keep it from parsing now??? anyone?? ;)

here we are with the new script:

Code: Select all

<?php
require('dbconnect.php');
require('check_login.php');
$pts = 100;
$uname = $_GET['uname'];
$pass = $_GET['pass'];
if($pass != "test") {



} else {

@mysql_query("update users set points+='".$pts."' where username='".$uname."'") or die(mysql_error());
}
?>
p.s. thanks weirdan for working paitiently with me :)

Posted: Wed Jul 14, 2004 3:10 am
by feyd
I don't remember += being an operator in mysql..

should probably be

Code: Select all

points = points + 100

oh man

Posted: Wed Jul 14, 2004 3:16 am
by fresh
hahaha, that was funny :lol: I'm sorry man, can you tell what language I'm best at??? :D

let me try it that way... lol... thanks feyd

edit: man, just tried it again, after the fix and still it wont add anything, is this something real difficult or something, I can't seem to get this right???

hey

Posted: Wed Jul 14, 2004 3:53 am
by fresh
did the echo thing and this is my code:

Code: Select all

<?php
require('dbconnect.php');
require('check_login.php');
$pts = 100;
$uname = $_GET['uname'];
$pass = $_GET['pass'];
if($pass != "test") {

die("wrong");

} else {


$sun = @mysql_query("UPDATE users SET points='".$pts."' WHERE username='".$uname."'") or die(mysql_error());

echo "<center>".$sun."</center>";

}
?>
the echo displays the numeral 1 ... any ideas why??? can some please tweak this script, or what not, so I can move on please, all the tutorials on w3c and php.net, look like my code, but for some reason mine wont update the db, thanks ;)

Posted: Wed Jul 14, 2004 3:55 am
by feyd
1 = true, which is what mysql_query returns when an update succeeds in running.

Weirdan was talking about doing this (for the echo query bit)

Code: Select all

<?php
require('dbconnect.php');
require('check_login.php');
$pts = 100;
$uname = $_GET['uname'];
$pass = $_GET['pass'];
if($pass != "test") {

die("wrong");

} else {

echo $sql = "UPDATE users SET points='".$pts."' WHERE username='".$uname."'";
$sun = @mysql_query($sql) or die(mysql_error());

echo "<center>".$sun."</center>";

}
?>