Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to make sure, username and password doesn't get duplicated?
QtWS25 Last Chance

how to make sure, username and password doesn't get duplicated?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 532 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sapok_5
    wrote on last edited by
    #1

    #include "idcreator.h"
    #include "ui_idcreator.h"
    #include<QMessageBox>
    #include"mainwindow.h"
    #include "voterid.h"
    #include"QtWidgets"
    MainWindow *h2;
    VoterID *h;
    idcreator::idcreator(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::idcreator)
    {
    ui->setupUi(this);
    mydb=QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("C:/SQLite/Database.db");
    if(!mydb.open())
    ui->connect->setText("Something is wrong, try restarting");
    else
    ui->connect->setText("PLEASE SIGN UP");
    }

    idcreator::~idcreator()
    {
    delete ui;
    }

    void idcreator::on_save_clicked()
    {
    QString username, password;
    username=ui->username->text();
    password=ui->password->text();

       QSqlQuery query;
       query.prepare( "INSERT INTO signup (username, password) VALUES (?, ?)" );
       query.addBindValue(username);
       query.addBindValue(password);
              if(query.exec()){
    
                  QMessageBox::StandardButton reply = QMessageBox::question(this,"SignUp Successful","Use same username and password to vote ",
                                                                    QMessageBox:: Open | QMessageBox:: Cancel );
              if (reply==QMessageBox::Cancel){
              hide();
              h2= new MainWindow(this);
              h2->show();
              }
              else{
                  hide();
                  h=new VoterID(this);
                  h->show();
              }
    
    
              }
      else{
                  QMessageBox::critical(this,"SignUp Failed","Signup error, try again ");
              }
                      }
    
    JonBJ 1 Reply Last reply
    0
    • S Sapok_5

      #include "idcreator.h"
      #include "ui_idcreator.h"
      #include<QMessageBox>
      #include"mainwindow.h"
      #include "voterid.h"
      #include"QtWidgets"
      MainWindow *h2;
      VoterID *h;
      idcreator::idcreator(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::idcreator)
      {
      ui->setupUi(this);
      mydb=QSqlDatabase::addDatabase("QSQLITE");
      mydb.setDatabaseName("C:/SQLite/Database.db");
      if(!mydb.open())
      ui->connect->setText("Something is wrong, try restarting");
      else
      ui->connect->setText("PLEASE SIGN UP");
      }

      idcreator::~idcreator()
      {
      delete ui;
      }

      void idcreator::on_save_clicked()
      {
      QString username, password;
      username=ui->username->text();
      password=ui->password->text();

         QSqlQuery query;
         query.prepare( "INSERT INTO signup (username, password) VALUES (?, ?)" );
         query.addBindValue(username);
         query.addBindValue(password);
                if(query.exec()){
      
                    QMessageBox::StandardButton reply = QMessageBox::question(this,"SignUp Successful","Use same username and password to vote ",
                                                                      QMessageBox:: Open | QMessageBox:: Cancel );
                if (reply==QMessageBox::Cancel){
                hide();
                h2= new MainWindow(this);
                h2->show();
                }
                else{
                    hide();
                    h=new VoterID(this);
                    h->show();
                }
      
      
                }
        else{
                    QMessageBox::critical(this,"SignUp Failed","Signup error, try again ");
                }
                        }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Sapok_5
      Hello and welcome.

      When pasting blocks of code, please use the Code tag you can see when composing a post (looks like </>), or type a line with 3-backticks (```) both above & below the code block.

      If mean you want to ensure that the username and/or password are not already in the signup table then first issue a SELECT query to check the value(s) are not already there before executing the INSERT statement.

      If username is the primary key into the signup table (which it probably/ought to be) you will not be able to INSERT another row with a username which is already there anyway --- the INSERT would return an error.

      As a separate issue:

      mydb=QSqlDatabase::addDatabase("QSQLITE");
      

      Your mydb must be member variable in idcreator class. The docs for QSqlDatabase tell you not to keep an instance in existence in a class:

      Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

      S 1 Reply Last reply
      1
      • A.A.SEZENA Offline
        A.A.SEZENA Offline
        A.A.SEZEN
        wrote on last edited by
        #3

        You may be looking for the unique type.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          For username, as @A-A-SEZEN suggests, use the unique keyword. As for the password, the fact that several of your users may have the same is not your problem. However, the real issue you have here is that you are storing password as plain text. This is wrong on so many level that I will not try to count. Please start directly with the proper logique to encrypt them.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • JonBJ JonB

            @Sapok_5
            Hello and welcome.

            When pasting blocks of code, please use the Code tag you can see when composing a post (looks like </>), or type a line with 3-backticks (```) both above & below the code block.

            If mean you want to ensure that the username and/or password are not already in the signup table then first issue a SELECT query to check the value(s) are not already there before executing the INSERT statement.

            If username is the primary key into the signup table (which it probably/ought to be) you will not be able to INSERT another row with a username which is already there anyway --- the INSERT would return an error.

            As a separate issue:

            mydb=QSqlDatabase::addDatabase("QSQLITE");
            

            Your mydb must be member variable in idcreator class. The docs for QSqlDatabase tell you not to keep an instance in existence in a class:

            Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

            S Offline
            S Offline
            Sapok_5
            wrote on last edited by
            #5

            @JonB i'm totally new to this. i don't know much about QT. if you can, please send me syntax to select query.check,
            i searched but i couldn't find expected result.

            JonBJ 1 Reply Last reply
            0
            • S Sapok_5

              @JonB i'm totally new to this. i don't know much about QT. if you can, please send me syntax to select query.check,
              i searched but i couldn't find expected result.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Sapok_5
              Writing the text of the query requires SQL knowledge, not Qt knowledge. You will need that (or at least the basics) to get anywhere with database programming.

              If, say, the user enters a username and you wanted to know if there is already a row in the table with that username you might do something like:

                 query.prepare( "SELECT username, password FROM signup WHERE username = ?" );
                 query.addBindValue(username);
                 if (query.exec())
                    ...
              

              Either it returns an existing row with that same username, or it returns no rows, so that's how you know.

              However, as we have said earlier, if (in SQL) you declare the username column in the table as either the primary key or unique it would not allow you to INSERT a new row with a duplicate username value, it would raise an error instead an not insert the new row. You do not want to allow a duplicate username, so you ought do one of these to maintain the integrity of your data.

              S 1 Reply Last reply
              1
              • JonBJ JonB

                @Sapok_5
                Writing the text of the query requires SQL knowledge, not Qt knowledge. You will need that (or at least the basics) to get anywhere with database programming.

                If, say, the user enters a username and you wanted to know if there is already a row in the table with that username you might do something like:

                   query.prepare( "SELECT username, password FROM signup WHERE username = ?" );
                   query.addBindValue(username);
                   if (query.exec())
                      ...
                

                Either it returns an existing row with that same username, or it returns no rows, so that's how you know.

                However, as we have said earlier, if (in SQL) you declare the username column in the table as either the primary key or unique it would not allow you to INSERT a new row with a duplicate username value, it would raise an error instead an not insert the new row. You do not want to allow a duplicate username, so you ought do one of these to maintain the integrity of your data.

                S Offline
                S Offline
                Sapok_5
                wrote on last edited by
                #7

                @JonB
                #include "idcreator.h"
                #include "ui_idcreator.h"
                #include<QMessageBox>
                #include"mainwindow.h"
                #include"voteme.h"
                MainWindow *h2;
                voteme *vm;
                idcreator::idcreator(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::idcreator)
                {
                ui->setupUi(this);
                mydb=QSqlDatabase::addDatabase("QSQLITE");
                mydb.setDatabaseName("C:/SQLite/Database.db");
                if(!mydb.open())
                ui->connect->setText("Something is wrong, try restarting");
                else
                ui->connect->setText("PLEASE SIGN UP");
                }

                idcreator::~idcreator()
                {
                delete ui;
                }
                int i;
                void idcreator::on_save_clicked()
                {
                QString username, password;
                username=ui->username->text();
                password=ui->password->text();

                   QSqlQuery query;
                   query.prepare( "SELECT *FROM signup WHERE username = '"+username+"' ");
                   if(query.exec()){
                       if(username==""||password==""){
                           QMessageBox::critical(this,"SignUp Failed","Please Enter Username or password ");
                
                       }
                       else{
                       if(query.next()){
                           QMessageBox::critical(this,"SignUp Failed","Enter different Username ");
                       }
                
                   else{
                
                
                    query.prepare( "INSERT INTO signup (username, password) VALUES (?, ?)" );
                       query.addBindValue(username);
                           query.addBindValue(password);
                
                                  if(query.exec()){
                                       QMessageBox::StandardButton reply = QMessageBox::question(this,"SignUp Successful","Use same username and password to vote ",
                                                                                                              QMessageBox:: Open | QMessageBox:: Cancel );
                                                  if (reply==QMessageBox::Cancel){
                                                 hide();
                                                  delete h2;
                                                  h2=new MainWindow(this);
                                                  h2->show();
                                                            }
                                                            else{
                                                            hide();
                                                              vm=new voteme(this);
                                                              vm->show();
                                                          }
                                                            }
                                                    else{
                                                                QMessageBox::critical(this,"SignUp Failed","Signup error, try again ");
                                                            }
                                          ui->connect->setText("verified");
                
                   }
                
                       }
                   }
                

                }

                i did this, and i reduced the duplication problem, however i still fell something is missing. it works perfectly fine, hope u can tell me something that would help reduce trouble for my application.

                JonBJ SGaistS 2 Replies Last reply
                0
                • S Sapok_5

                  @JonB
                  #include "idcreator.h"
                  #include "ui_idcreator.h"
                  #include<QMessageBox>
                  #include"mainwindow.h"
                  #include"voteme.h"
                  MainWindow *h2;
                  voteme *vm;
                  idcreator::idcreator(QWidget *parent) :
                  QDialog(parent),
                  ui(new Ui::idcreator)
                  {
                  ui->setupUi(this);
                  mydb=QSqlDatabase::addDatabase("QSQLITE");
                  mydb.setDatabaseName("C:/SQLite/Database.db");
                  if(!mydb.open())
                  ui->connect->setText("Something is wrong, try restarting");
                  else
                  ui->connect->setText("PLEASE SIGN UP");
                  }

                  idcreator::~idcreator()
                  {
                  delete ui;
                  }
                  int i;
                  void idcreator::on_save_clicked()
                  {
                  QString username, password;
                  username=ui->username->text();
                  password=ui->password->text();

                     QSqlQuery query;
                     query.prepare( "SELECT *FROM signup WHERE username = '"+username+"' ");
                     if(query.exec()){
                         if(username==""||password==""){
                             QMessageBox::critical(this,"SignUp Failed","Please Enter Username or password ");
                  
                         }
                         else{
                         if(query.next()){
                             QMessageBox::critical(this,"SignUp Failed","Enter different Username ");
                         }
                  
                     else{
                  
                  
                      query.prepare( "INSERT INTO signup (username, password) VALUES (?, ?)" );
                         query.addBindValue(username);
                             query.addBindValue(password);
                  
                                    if(query.exec()){
                                         QMessageBox::StandardButton reply = QMessageBox::question(this,"SignUp Successful","Use same username and password to vote ",
                                                                                                                QMessageBox:: Open | QMessageBox:: Cancel );
                                                    if (reply==QMessageBox::Cancel){
                                                   hide();
                                                    delete h2;
                                                    h2=new MainWindow(this);
                                                    h2->show();
                                                              }
                                                              else{
                                                              hide();
                                                                vm=new voteme(this);
                                                                vm->show();
                                                            }
                                                              }
                                                      else{
                                                                  QMessageBox::critical(this,"SignUp Failed","Signup error, try again ");
                                                              }
                                            ui->connect->setText("verified");
                  
                     }
                  
                         }
                     }
                  

                  }

                  i did this, and i reduced the duplication problem, however i still fell something is missing. it works perfectly fine, hope u can tell me something that would help reduce trouble for my application.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Sapok_5
                  I would not expect this to work, to tell you whether there is actually a row with the username in existence. You execute the SELECT query, but you do nothing about reading the resulting rows back. You need to do so to see whether the SELECT returns 0 or 1 rows.

                  1 Reply Last reply
                  1
                  • S Sapok_5

                    @JonB
                    #include "idcreator.h"
                    #include "ui_idcreator.h"
                    #include<QMessageBox>
                    #include"mainwindow.h"
                    #include"voteme.h"
                    MainWindow *h2;
                    voteme *vm;
                    idcreator::idcreator(QWidget *parent) :
                    QDialog(parent),
                    ui(new Ui::idcreator)
                    {
                    ui->setupUi(this);
                    mydb=QSqlDatabase::addDatabase("QSQLITE");
                    mydb.setDatabaseName("C:/SQLite/Database.db");
                    if(!mydb.open())
                    ui->connect->setText("Something is wrong, try restarting");
                    else
                    ui->connect->setText("PLEASE SIGN UP");
                    }

                    idcreator::~idcreator()
                    {
                    delete ui;
                    }
                    int i;
                    void idcreator::on_save_clicked()
                    {
                    QString username, password;
                    username=ui->username->text();
                    password=ui->password->text();

                       QSqlQuery query;
                       query.prepare( "SELECT *FROM signup WHERE username = '"+username+"' ");
                       if(query.exec()){
                           if(username==""||password==""){
                               QMessageBox::critical(this,"SignUp Failed","Please Enter Username or password ");
                    
                           }
                           else{
                           if(query.next()){
                               QMessageBox::critical(this,"SignUp Failed","Enter different Username ");
                           }
                    
                       else{
                    
                    
                        query.prepare( "INSERT INTO signup (username, password) VALUES (?, ?)" );
                           query.addBindValue(username);
                               query.addBindValue(password);
                    
                                      if(query.exec()){
                                           QMessageBox::StandardButton reply = QMessageBox::question(this,"SignUp Successful","Use same username and password to vote ",
                                                                                                                  QMessageBox:: Open | QMessageBox:: Cancel );
                                                      if (reply==QMessageBox::Cancel){
                                                     hide();
                                                      delete h2;
                                                      h2=new MainWindow(this);
                                                      h2->show();
                                                                }
                                                                else{
                                                                hide();
                                                                  vm=new voteme(this);
                                                                  vm->show();
                                                              }
                                                                }
                                                        else{
                                                                    QMessageBox::critical(this,"SignUp Failed","Signup error, try again ");
                                                                }
                                              ui->connect->setText("verified");
                    
                       }
                    
                           }
                       }
                    

                    }

                    i did this, and i reduced the duplication problem, however i still fell something is missing. it works perfectly fine, hope u can tell me something that would help reduce trouble for my application.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Sapok_5 you really should take the time to learn the basics of the tools you want to wield. You are doing your empty checks too late (your users should not even be allowed to click on the button if any of the fields are empty). Building the query the way you do it opens the door to SQL injection and all the nasty things that come with it.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved