Closed Thread
Results 1 to 8 of 8

Thread: Not able to update database

  1. #1
    oiwio is offline x10 Sophmore oiwio is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    214

    Not able to update database

    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?

  2. #2
    Brandon's Avatar
    Brandon is offline Former Senior Account Rep Brandon is on a distinguished road
    Join Date
    Jun 2006
    Location
    Tewksbury, MA
    Posts
    9,589

    Re: Not able to update database

    Are you getting any errors?
    Thanks,
    Brandon Long

  3. #3
    oiwio is offline x10 Sophmore oiwio is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    214

    Re: Not able to update database

    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 by oiwio; 12-09-2007 at 02:02 PM.

  4. #4
    SyncViews is offline x10Hosting Member SyncViews is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    37

    Re: Not able to update database

    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.

  5. #5
    oiwio is offline x10 Sophmore oiwio is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    214

    Re: Not able to update database

    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

  6. #6
    Russ's Avatar
    Russ is offline Retired ***** Russ is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    3,162

    Re: Not able to update database

    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 by Russ; 12-09-2007 at 08:13 PM.
    x10 Chat is now available! Live Support!

    DO NOT PRIVATE MESSAGE STAFF - WE ARE BUSY

  7. #7
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: Not able to update database

    Quote Originally Posted by rpope904 View Post
    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:
    function updateUserField($table, $username, $field, $value){
    $q = "UPDATE $table SET $field = $value WHERE username = $username";
     return mysql_query($q, $this->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 by Livewire; 12-09-2007 at 08:32 PM.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  8. #8
    oiwio is offline x10 Sophmore oiwio is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    214

    Re: Not able to update database

    I got it working, but if something goes wrong again Ill tell you. Otherwise, thank you guys for all the help
    Last edited by oiwio; 12-10-2007 at 12:16 AM.

Closed Thread

Similar Threads

  1. any idea??
    By galaxyAbstractor in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 11-12-2007, 12:24 PM
  2. PHP BB 2.0.16 Manual instalation
    By GFIV in forum Free Hosting
    Replies: 8
    Last Post: 09-14-2005, 12:40 PM
  3. New PSP UPGADE
    By IamShipon1988 in forum Gamer's Lounge
    Replies: 4
    Last Post: 09-04-2005, 08:23 PM
  4. Database Mysql
    By TheCentaury in forum Free Hosting
    Replies: 3
    Last Post: 08-23-2005, 09:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers