0

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");
}
?>
2
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by mikulucky
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 :)

0

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.

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.