Not able to update database

Status
Not open for further replies.

oiwio

New Member
Messages
214
Reaction score
0
Points
0
ok, so Im using the code:
function updateUserField($table, $username, $field, $value){
$q = "UPDATE $table SET $field = $value WHERE username = $username";
return mysql_query($q, $this->connection);
}

But the problem is that it will only update numbers into the database but when I try to set a field to a variable value it wont update. is there another command or something that Im missing that will allow me to update a field with a variable?
 

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Are you getting any errors?
 

oiwio

New Member
Messages
214
Reaction score
0
Points
0
no, but when I run the function to update something using a value with a letter, it doesnt update, but if I use the same function but with only a # value, it will update

And Im trying to update a varchar so it should be updating.
 
Last edited:

SyncViews

New Member
Messages
37
Reaction score
0
Points
0
Use:
$return = mysql_query($q, $this->connection) or exit (mysql_error());

If it fails for any reason it should output an sql error and stop the script.
 

oiwio

New Member
Messages
214
Reaction score
0
Points
0
ok, Im now getting a:
Unknown column 'moose1137' in 'where clause'

the thing is is that I cant find a "clause" anywhere in the script or database
 

Russ

<b>Retired *****</b>
Messages
3,168
Reaction score
2
Points
38
The WHERE clause is used to specify a selection criterion.
The WHERE Clause

To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.
Syntax

SELECT column FROM table
WHERE column operator value


So.. In your script, locate the
WHERE moose1137 * *
part is, and make sure the database column is infact, moose1137.
 
Last edited:

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
So.. In your script, locate the
WHERE moose1137 * *
part is, and make sure the database column is infact, moose1137.

I see the problem actually.

Code:
[U]function[/U] updateUserField($table, $username, $field, $value){
$q = "UPDATE $table SET $field = $value WHERE username = $username";
 [U]return[/U] mysql_query($q, $[U]this[/U]->connection);
 }
username = $username; you need some quotes or single quotes on that. Whats happening is $username IS being turned into moose1337 like it's supposed to.

But without quotes, MySQL thinks you're trying to update the username field with the contents of the column moose1337.

Try this:

$q = "UPDATE $table SET $field = $value WHERE username = '" . $username . "';";

I know thats some ugly code, but what it boils down to is those quotes by $username - they're actually single quotes with a double right after them, like this:

' " . $username . " '

The space is in there to show theres a single before the double, do NOT include the space or you'll have an extra space at the beginning and end of the username!
That will turn your query into:

UPDATE table SET field=value WHERE username = 'moose1337'
Which is what you want :)


Edit: FYI my php might be a bit rusty. If I'm right, using " . $username . " should basically have PHP put the value of $username into the string. Try it with just '$username' and see if that works too, cause it'll save typing if it does :)

In any case, the general idea should be right - MySQL needs some quotes so it knows moose1337 is a value, not a column name.
 
Last edited:

oiwio

New Member
Messages
214
Reaction score
0
Points
0
I got it working, but if something goes wrong again Ill tell you. Otherwise, thank you guys for all the help
 
Last edited:
Status
Not open for further replies.
Top