user1393955
user1393955

Reputation: 27

MySQL PHP Query Invalid - Can't find column

UPDATE: The suggestion of the '' worked around $username. Thanks! But now, the table isn't actually getting updated from the $lastLoginTime.

I have some problem with my query that I can't seem to figure out for the life of me and I am at a halt.

Let's take a look at the code.

function checkTOS($preusergrab){
include("includes/opendb.php");

$query = "SELECT * FROM users WHERE username='".$preusergrab."'";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){
    $resultset[] = $row;
    $TOS = $row['acceptTOS'];
    }

mysql_free_result($result);

 if($TOS == 1){
 // return to processor

    $lastLoginTime = time();

    $query = mysql_query("UPDATE users 
                    SET lastLoginTime = '$lastLoginTime' 
                        WHERE username = $username");

                        if (!$query) {

             die('<br>Invalid query: ' . mysql_error());
            }


 }elseif($TOS == 0 || $TOS = ''){
    header("Location: http://partner.domain.com/terms.php?action=show");
    die();
}else{
echo 'Internal Application Error:';
echo 'Value unrecognizable.';
echo '<br>Please alert someone at [email protected]';
die();
}
 }

Now, where the problem comes in is at this section:

    $lastLoginTime = time();

    $query = mysql_query("UPDATE users 
                    SET lastLoginTime = '$lastLoginTime' 
                        WHERE username = $username");

                        if (!$query) {

             die('<br>Invalid query: ' . mysql_error());
            }

It says the following:

   Invalid query: Unknown column 'theuser' in 'where clause'

In this case, 'theuser' is the user that $preusergrab is representing.

In my table, the username is the primary key, row 0.

What could possibly be invalid if I know the row is there and everything else works?

Upvotes: 1

Views: 1225

Answers (3)

Ikmal Ezzani
Ikmal Ezzani

Reputation: 1283

I believe you forgot to quote the $username, quote like so '$username'

$query = mysql_query("UPDATE users 
SET lastLoginTime = '$lastLoginTime' 
WHERE username = '$username'");

if (!$query) {
die('<br>Invalid query: ' . mysql_error());
}

Upvotes: 0

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171381

It should be:

WHERE username = '$username'"); 

Note the apostrophes I added around your $username' variable.

Upvotes: 1

triclosan
triclosan

Reputation: 5714

try to

$query = mysql_query("UPDATE users 
                SET lastLoginTime = '$lastLoginTime' 
                    WHERE username = '$username'");

Upvotes: 2

Related Questions