Need help with MFC dialog- login page

I have this in my login button but it seems to have bugs here. When valid username n password is entered it login to the other dialog which is correct and working. but when a invalid user is key in it will show message that invalid login but after that i try to enter a valid user it will keep prompt me invalid login although i entered the correct one. i looks like my login form only work once. need help to solve this error im not sure where i go wrong. im quite new to C++ MFC dialog still. Thank you for you helps

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void CPeopleCounterDlg::OnBnClickedbtnlogin()
	{

	UpdateData();
	bool ValidLogin = false;
	CString ss;
	int count=0;

	if( m_Username == "" )
		{
		AfxMessageBox("You must provide a username to Login.");
		m_EditUsername.SetFocus();
		return;
		}
	if( m_Password == "" )
		{
		AfxMessageBox("Please enter your password.");
		m_EditPassword.SetFocus();
		return;
		}
	
	ss.Format("SELECT * FROM user WHERE Username ='%s' AND Password = '%s'", m_Username, m_Password);
	mysql_query(ssock,ss);
	res = mysql_store_result(ssock);
	if(!res){
		MessageBox("Username and Password is in-correct");
		AfxGetMainWnd()->Invalidate();
		return;
		} 
	while(row = mysql_fetch_row(res))
		{
		count=count+1;
		}
		if(count==1){
			this->ShowWindow(SW_HIDE);
			this->ShowWindow(SW_SHOW);
			CPplCMain  m_pplc;
			m_pplc.DoModal();
			}
		else if(count>1){
					 MessageBox("Duplicate Username and Password.... Access denied");
					 }
				 else
					 MessageBox("Username and Password is in-correct");
	UpdateData(FALSE);
	mysql_close(ssock);
	}
when a invalid user is key in it will show message that invalid login but after that i try to enter a valid user it will keep prompt me invalid login although i entered the correct one

That's because you told it to (with count).
1
2
3
4
5
6
7
8
9
10
11
		if(count==1){
			this->ShowWindow(SW_HIDE);
			this->ShowWindow(SW_SHOW);
			CPplCMain  m_pplc;
			m_pplc.DoModal();
			}
		else if(count>1){
					 MessageBox("Duplicate Username and Password.... Access denied");
					 }
				 else
					 MessageBox("Username and Password is in-correct");
Last edited on
I'd say that on line 25 res is not NULL (that would indicate an error).

Use mysql_num_rows(...) in order to determine whether the combination exist or not
@coder777 How do i fix this error ? i am totally C++ MFC dumb here lol don know how to use mysql_num_rows etc. do you have exact resource code on this please. i have never done this before need some examples to learn from. i understand from codes better than descriptions lol.
i tried to change to mysql_num_rows it seems working but when i type in valid user visual studio bring me back to coding prompt to while(row = mysql_fetch_row(res)) some violation error.

@kbw i use the count to determine duplicate users and i transfer the concept from C# or somewhere i rmb. if it is incorrect then how to i change it to ? any examples or you have some codes for me thanks
Last edited on
Show your modified code.

mysql_num_rows(...) should suffice to determine whether the user/password combination exists. Check for duplicates when a new user wants to register
OK done thanks guys i found that the error lies with the mysql_close -.- i randomly remove it and it works with those codes. also i change to mysql_num_rows thanks so much
Topic archived. No new replies allowed.