简体   繁体   中英

PHP / MySQL - update table data with session data

How to update table data with data stored in session?

I have following code but it is not working(INSERTING INTO DB not working exactly). Please not that this is just part of the code. Session value are being saved correctly. I have a problem with saving them though.

If I have made silly mistake - apologies - I'm newby.

    $loggedTime = $_SESSION['loggedtime'];
    $thisUser = $_SESSION['usr'];

    $result = mysql_query("UPDATE 'admin' SET dt = $loggedTime WHERE $thisuser");
    if($result) {
         echo "success"; 
    } else { 
        echo "no success"; 
    }
"UPDATE `admin` SET `dt` = '$loggedTime' WHERE user = '$thisUser'"

Don't use single quotes around column/table names. Single/double quotes indicate string literals. Use back ticks around table/column names, if you have to (ie reserved word). Also, have a look at Bobby Tables . Furthermore, you need a column in the where clause.

UPDATE 'admin'

replace the single quote

UPDATE admin SET ...

Understand lots of people tend to use back-tick to quote the table, column name... i don't think is necessary

just take a look on the mysql docs
THEY never do that

Try to explicitly name the column in your WHERE clause and surround variables with single quotes.

"UPDATE `admin` SET dt = '$loggedTime' WHERE user = '$thisuser'"

Theres a problem with your SQL query.

UPDATE 'admin' SET dt = $loggedTime WHERE $thisuser

The where clause isn't specifying any condition try:

UPDATE 'admin' SET dt = '$loggedTime' WHERE userField = $thisuser

replacing 'userField' with a relavent field name that you would like to condition for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM