954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP MYSQL UPDATE not working

So I'm having this weird issue, where I can do SELECT statements and INSERT statements just fine, but when I do this UPDATE statement, it sorta works. It runs without errors, and even outputs that 1 row is affected. But when I check the MySQL, nothing is updated. I've echo-ed out the UPDATE statement, which has all variables being passed to it correctly. When I take that echo-ed statement and input directly into MySQL, it works fine.

I've double checked that $login_count is actually an INT Value. I've even tried converting the statement into an "INSERT ON DUPLICATE KEY UPDATE" and that has the exact same results. Am I missing something??

<?php
session_start();

require_once ('../../database_connection.php');

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];
$encrypted_mypassword=md5($mypassword);

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$myusername = mysql_real_escape_string($myusername);

$query_user_login = mysql_query ("SELECT * FROM members WHERE username='$myusername' and password='$encrypted_mypassword'");

// Mysql_num_row is counting table row
if(mysql_num_rows($query_user_login)==1)
// If result matched $myusername and $mypassword, table row must be 1 row
{
	// Register $myusername, $mypassword and redirect to file "login_success.php"
	$user_array = mysql_fetch_array($query_user_login);
	$memberid = $user_array['memberid'];
	$login_count = $user_array['login_count'] + 1;
	
	$last_login = date('Y-m-d H:i:s');
	
	$query_update_last_login = ("UPDATE `members` set `login_count` = $login_count where `memberid`=$memberid") or die(mysql_error());
	
	mysql_close(); // Close the database connection.
	
	$_SESSION['memberid'] = $user_array['memberid'];
	$_SESSION['username'] = $user_array['username'];
	
	header("location:../../index.php");
}
else 
{
	echo mysql_error();
	
	mysql_close(); // Close the database connection.
	
	header("location:../../index.php?action=bad_login");
}
?>
NullReturned
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Is this line

$query_update_last_login = ("UPDATE `members` set `login_count` = $login_count where `memberid`=$memberid") or die(mysql_error());


You are not actually executing a query change this line to the following:

$query_update_last_login = "UPDATE `members` set `login_count` = $login_count where `memberid`=$memberid";

mysql_query($query_update_last_login ) or die(mysql_error());


Hope that helps :)

mikulucky
Junior Poster in Training
85 posts since Jan 2012
Reputation Points: 41
Solved Threads: 13
 

GAH! Thanks mikulucky! It's so simple and obvious, but I was focusing on the statement and forgot to format it to run the query. I feel so stupid, but thanks for the second set of eyes.

NullReturned
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

No problem, it is easy to get caught up in other things. If it has worked please mark thread as solved and give rep as appropriate.

Thanks

mikulucky
Junior Poster in Training
85 posts since Jan 2012
Reputation Points: 41
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: