Search code examples
phpsqlmysqlmysql-error-1054

Error when trying an additional SET command in query


The MySQL query below works well as it is. It replaces the field votes_up in a MySQL database with whatever value there is for the variable $votes_up.

UPDATE submission 
   SET votes_up = $votes_up 
 WHERE submissionid = $id

However, when I try to add a second condition that would simultaneously replace a field called flag1 with the value of a variable called $uflag, I get an error message. The query I'm trying to use for this is below. The error message says Unknown column 'admin' in 'field list' if the value of $uflag is "admin". Also, the value of $uflag is not being put into the database. Any ideas why I am getting this error?

UPDATE submission
   SET votes_up = $votes_up, 
       flag1 = $uflag 
 WHERE submissionid = $id

Solution

  • You need to add quotes to your string values:

    UPDATE submission 
    SET votes_up = $votes_up, flag1 = $uflag 
    WHERE submissionid = $id
    

    Should be:

    UPDATE submission 
    SET votes_up = $votes_up, flag1 = '$uflag' 
    WHERE submissionid = $id