0

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"; 
    }
5
  • please enlighten us what is NOT WORKING Commented Aug 11, 2011 at 14:41
  • Never output a fixed string while debugging data base problems, it's utterly useless. Always use mysql_error() to tell you exactly WHY a query failed. Commented Aug 11, 2011 at 14:43
  • @Marc - can you give ma an example please? Currently my statement is returning 'no success' so something is wrong. Commented Aug 11, 2011 at 14:49
  • $res = mysql_query(...) or die(mysql_error()) is the quickest/easiest form. Commented Aug 11, 2011 at 14:51
  • I did: echo mysql_error()."<br />"; and has returned You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '15:45:32 WHERE usr =' at line 1 Commented Aug 11, 2011 at 14:52

4 Answers 4

5
"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 (i.e. reserved word). Also, have a look at Bobby Tables. Furthermore, you need a column in the where clause.

Sign up to request clarification or add additional context in comments.

4 Comments

after update I'm getting an error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '15:45:32 WHERE usr =' at line 1
@NewUSer typo in variable name. Updated it.
@cularis - updated, but still an error full code here jsfiddle.net/MrTest/GSAk6
You are not using the query provided in the answer. Single quotes around strings are missing.
3

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

2 Comments

I guess it started with phpMyAdmin adding back ticks to every object name, because people tend to use reserved words for tables and columns (desc as a shortcut for description for example). So everybody started putting back ticks around them.
rule number one, don't use reserved words (is reserved) for table name, column name :)
2

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

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

Comments

2

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.

1 Comment

^ this. and you might want to add curly brackets when you insert variables. ie. "UPDATE admin SET dt = {$loggedTime} WHERE someField = {$thisUser}" you know, just so we cool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.