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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

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

Post 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 ;)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

update users set points=points+$points where username = '$_SESSION&#1111;username]'
?
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

not working...

Post 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 ;)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$ php -l am.php
No syntax errors detected in am.php
Please post exact error message you've got.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

my error

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

that's it: $_SESSION['username'] is undefined or empty
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

well

Post 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 ;)
Last edited by fresh on Wed Jul 14, 2004 2:55 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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...
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

well

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

he was talking about echoing your sql query, just before you execute said query..
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

ohh sorry

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't remember += being an operator in mysql..

should probably be

Code: Select all

points = points + 100
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

oh man

Post 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???
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

hey

Post 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 ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>";

}
?>
Post Reply