updating a row using the session variables

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Red Midnight
Forum Newbie
Posts: 2
Joined: Mon Jun 30, 2003 4:29 pm

updating a row using the session variables

Post by Red Midnight »

--MySQL questions---

<?php
session_start();

$user=$_SESSION['user'];
$pass=$_SESSION['pass'];

$query="UPDATE users SET name=". $_SESSION['user'] .", password=". $_SESSION['pass'] .", strength=". $_SESSION['strength'] .", defense=". $_SESSION['defense'] .", powerlevel=". $_SESSION['powerlevel'] .", int=". $_SESSION['int'] .", hp=". $_SESSION['hp'] .", job=". $_SESSION['job'] .", gold=". $_SESSION['gold'] ." WHERE name=$user AND password=$pass";

$result=mysql_query($query) or die("Error in query:".mysql_error());

?>

I don't understand why this isnt working. It's giving me this---

Error in query:You have an error in your SQL syntax near 'int=1, hp=10, job=none, gold=40 WHERE name=Gohan AND password=e5e65fce9273c4762a' at line 1

can anyone please tell me what I typed wrong here? I've been sitting here forever to find out what I did wrong.
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

What are all the variables getting set to in your UPDATE statement? Is it possible that one of them contains a comma and is throwing things off? I generally put single quotes around all my variables when I do this. For example:

Code: Select all

$query="UPDATE users SET name='$_SESSION[user]', password='$_SESSION[pass]', strength='$_SESSION[strength]', defense='$_SESSION[defense]', powerlevel='$_SESSION[powerlevel]', int='$_SESSION[int]', hp='$_SESSION[hp]', job='$_SESSION[job]', gold='$_SESSION[gold]' WHERE name='$user' AND password='$pass'";
Red Midnight
Forum Newbie
Posts: 2
Joined: Mon Jun 30, 2003 4:29 pm

Post by Red Midnight »

I put in what you said and I get this:

Error in query:You have an error in your SQL syntax near 'int='1', hp='10', job='none', gold='40' WHERE name='Gohan' AND password='e5e65fc' at line 1

this thing is annoying
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

AH!! I think it just dawned on me. INT is a reserved word in MySQL to define a datatype. So it is getting confused when you are trying to use it in this SQL statement. Try changing the column name to something different and see if it works.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

This is a handy bookmark to use when designing tables (can prevent many headaches):
http://www.mysql.com/doc/en/Reserved_words.html

Mac
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

you will need quotes around the values who's db field is varchar and other text related ones
Post Reply