Jump to content

[SOLVED] How to Update an existing field PHP and MySQL - 4 lines of code.. UGH!


MesaFloyd

Recommended Posts

I am a layperson, please be gentle.

 

I have a login script that works fairly well. It sets cookies for people that login.

I would like to have that persons datetime field get updated whenever he revisits the site if his cookie is still active.

 

I added 4 lines of code that should do it, but does not<heavy sigh>... the result is a blank screen, I remove the lines and all is back and OK.

 

4 lines of code... simple right???? not for me.... ;-)

 

Here is the code.. I added a few lines before and after so you can see the setup...

My problem lines are numbered #1...#4...

 

Thanks in advance

function displayLogin()
{
   global $logged_in;
   if($logged_in)
   {
        //FK----attempt to update datetime field for people that log visits to the site
//	$query = "UPDATE users SET datetime=date("Y.m.d - H:i:s"),  //Line#1
//	WHERE username='$_SESSION[username]'"; 		//Line#2

//	$result = mysql_query($conn, $query)			//Line#3
//		or die ("could not execute the query.");    	//Line#4
         //FK-----attempt end 

    echo "<br><h1>Logged In!<br><br></h1>";
echo "Hello <b>$_SESSION[username]</b> you are logged in.<br>

 

You can see the // where I been tesing to control those lines

(Maybe you can also tell me how to disply the error message rather than just a blank screen)

Any help would be very appreciated.. please be simple in you explanations

Thanks in advance.

Link to comment
Share on other sites

Hi Blade, thanks for looking,

 

I replaced my four lines of code with your code (without your <?php and  ?> )

 

...still a Blank screen....

 

Just for a sanity check, I // out your and my code, and goes back to working

 

my mind is 'boggled'

Link to comment
Share on other sites

<?php
function displayLogin()
{
   global $logged_in;
   if($logged_in)
   {
mysql_connect($conn) or die('Could not connect: ' . mysql_error());
$date = date("Y.m.d - H:i:s");
$sql_update = "UPDATE users SET datetime='{$date}' WHERE username='{$_SESSION['username']}';";
mysql_query($sql_update)  or die ("could not execute the query:". mysql_error());

    echo "<br><h1>Logged In!<br><br></h1>";
echo "Hello <b>$_SESSION[username]</b> you are logged in.<br>";
   }
}
displayLogin();

?>

sorry, Blade. Your code was kinda messed up. No offense.

Link to comment
Share on other sites

Blade and Jonsjava

Thanks to you both.

Your scripting was very helpful.... I did a bit of adjsting for my code and works now

I appreciate both your cleverness....

Jons... I had already referred to the DB so I didnt need your connect method I just needed the way you posted the date to mysql... worked like a charm

 

 

Eternally grateful

Floyd

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.