Hack Forums
[C] MySQL login - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] MySQL login (/showthread.php?tid=5788139)



MySQL login - ReBeL_1 - Jan 21, 2018

Trying to implement a login system, I'm struggling to get it working. I'm using Qt and MySQL - the MySQL link is functioning fine, just can't get the C++ right. I understand that the system will be susceptible to sql injections - I'm fine with that (for now), as my project involves showing progress. Here's the code:

Code
void MainWindow::on_pushButton_login_clicked()
{

   QString enteredUsername = ui->lineEdit_username->text();
   QString enteredPassword = ui->lineEdit_password->text();
   QString username;
   QString password;


   QSqlQuery loginQuery("SELECT * FROM 'logindetails' WHERE studentID='" + enteredUsername + "' AND password='" + enteredPassword + "';");
   if (!username.compare(enteredUsername) && (!password.compare(enteredPassword)))
       QMessageBox::information(this,"Success", "Login information is correct");
   else
       QMessageBox::information(this,"FAIL", "Incorrect login");

/*

   QSqlQuery usernameQuery("SELECT studentID from userinfo.logindetails");
   while (usernameQuery.next()) {
       QString username = usernameQuery.value(0).toString();
       QMessageBox::information(this,"Success",username+ enteredUsername);

   }
   QSqlQuery passwordQuery("SELECT password from userinfo.logindetails");
   while (passwordQuery.next()) {
       QString password = passwordQuery.value(0).toString();
       QMessageBox::information(this,"Success",password+ typeid(password).name()+ enteredPassword+ typeid(enteredPassword).name());

   }

   if (!username.compare(enteredUsername) && (!username.compare(enteredUsername)))
           QMessageBox::information(this,"Success", "Login information is correct");

   else
       QMessageBox::information(this,"Failure", "Login information is incorrect"+username + password);

*/



RE: MySQL login - HFDone - Jan 21, 2018

i think your problem is the single quotes http://doc.qt.io/qt-5/sql-sqlstatements.html


RE: MySQL login - ReBeL_1 - Jan 21, 2018

Querying the DB hasn't been the problem, it returns the correct username and password but they check is at fault.


RE: MySQL login - ZwClose - Jan 22, 2018

Try strncmp instead, it's both ANSI and Unicode, might be your problem.


RE: MySQL login - Irritiable - Feb 23, 2018

(Jan 21, 2018 07:41 AM)ReBeL_1 Wrote:  Querying the DB hasn't been the problem, it returns the correct username and password but they check is at fault.

Hello, ReBeL_1.

I'm unfamiliar with Qt personally, so take what I have to say with a grain of salt, please.

QString seems to be a C-style string. You are using C++.
You could avoid using the C functions and use a std::string in it's place.

Code
QString foo;
std::string myCPPfoo = foo; // Copy the QString into a C++ string

...

if (myCPPfoo == "string contents here") { /* do stuff here */ }

If what Cute Little Girl suggested is true: This will be an easier to read solution to the problem.

Sorry that I can't be of more assistance. 😖