1

I am trying to make a simple query that will update a user database with the time that the user last logged in. My SQL data base has several columns id | first_name | last_name | username| password | date_created | last_login The query I am trying to run is:

$sql="INSERT INTO (CLL_users) SET last_login= $dateCreated WHERE username= $username";
$result=mysql_query($sql);

Here is the entire code of the page loaded after successful validation and I do realize its the worst method to use because of SQL injection but once I get the proper query down I will recode it for PDO prepared statements this was just an example I found on another website and I am modifying it to add the last_login parameter.

<?php
session_start();
$host="localhost"; // Host name 
$username="user"; // Mysql username 
$password="XXXXX"; // Mysql password 
$db_name="DB"; // Database name 
$tbl_name="users"; // Table name 
date_default_timezone_set('America/Chicago');
$dateCreated = date('m/d/Y h:i:s a', time());

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="INSERT INTO (CLL_users) SET last_login= $dateCreated WHERE username= $username";
$result=mysql_query($sql);

if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
    <head>
        <title> Welcome</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <LINK href="CLL.css" rel="stylesheet" type="text/css">
    </head>
 <body>
     <?php echo $dateCreated ?>
<p>Login Successful</p>
</body>
</html>
3
  • You didn't really specify the problem you're having. Are you getting an error? If so, what error? Commented Nov 1, 2012 at 3:47
  • The problem is that it is not updating the database I am not getting any error and if I echo the variable that has the date stored in it, it will echo properly. I am unsure my query is correct for updating the database I have tried several different methods with no luck, Commented Nov 1, 2012 at 3:48
  • 1
    @Yamaha32088 INSERT create a new entry in database. For update you need to use UPDATE.. Commented Nov 1, 2012 at 3:57

5 Answers 5

2

You will need single quotes around you dateCreated and username variables.

Also, you need to use an update statement to update an existing record rather than doing an insert. If you echo the $sql variable you will see that your insert statement isn't valid and wont work if you try to run it manually on the database.

eg. $sql="UPDATE CLL_users SET last_login='$dateCreated' WHERE username='$username'";

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

Comments

2

Well first of all i will suggest to surround you variables with single quotes and next i will suggest you to use the following syntax for your insert. INSERT INTO table (COLUMN1, COLUMN2,COLUMN3) VALUES('value1','value2','value3')

Also replace INSERT INTO for UPDATE if what you want is update the row rather than insert something.

Comments

2
$sql="update CLL_users SET last_login= $dateCreated WHERE username= $username";
$result=mysql_query($sql);

Comments

2

Cause of error is, you dont have insert query with Set option if you would like to update the table follow this for sql variable

 $sql = "UPDATE CLL_users SET last_login= '$dateCreated' WHERE username='$username'";

Document For Insert Document for update

Comments

1

You can't use INSERT to update existing records in the table. Use UPDATE query

$sql="UPDATE CLL_users SET last_login= '$dateCreated' WHERE username= '$username'";

1 Comment

Thank you for the help I actually found more then just my error of using the INSERT statement. I overlooked the fact that I was calling the username variable of the database and not the username of the logged in user. Also I had an under_score between user and name in the database but my query was just "username".

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.