qt mysql database md5 password cheking problem - c++

I am making registration/login form with qt c++ saving registration information in mysql password is hashed by md5 also I've tried sha256 same there, so main problem it saves password hashed and I've got no problem with that but when i'm comparing it in login window it doesn't compares and password wrong message appears. without hashing everything works fine, with hashing checking problem. Thanks for help))
if (db.open()) {
QString email = ui->email->text();
QString password = QString("%1").arg(QString(QCryptographicHash::hash(ui->password->text().toUtf8(),QCryptographicHash::Md5).toHex()));
// Insert Query
QSqlQuery query(QSqlDatabase::database("MyConnection"));
query.prepare("SELECT * FROM users WHERE email = :email AND password = :password");
query.bindValue(":email", email);
query.bindValue(":password", password);
if (!query.exec()) {
QMessageBox::information(this,"Failed","Error please try again");
}
else {
QString emailLog = query.value(1).toString();
QString passwordLog = query.value(4).toString();
if (query.next()) {
QMessageBox::information(this,"SUCCESS","SUCCESS");
ui->plstryagain->close();
db.close();
} else {
QMessageBox::information(this,"Wrong","Wrong Password try again");
ui->plstryagain->show();
db.close();
}
}
}
else {
QMessageBox::information(this, "Database Error", "Can't Connect To Database");
}

Related

Retravel Database value according to the username and password Qt C++

I'm try to re-travel MYSQL database value to the QLine edit. I want to re-travel data according to the username and password. Username and password are in mainwindow.cpp. After login successfully, it's open second window named userdetails.cpp. I want to display data in this userdetails.cpp. This is the code I used in this moment.
void Userdetails::on_pushButton_4_clicked()
{
{
// database connection
QSqlDatabase database;
database = QSqlDatabase::addDatabase("QMYSQL","MyConnect");
database.setHostName("localhost");
database.setUserName("root");
database.setPassword("");
database.setDatabaseName("electricity");
if(database.open()) {
QSqlQuery query(database);
if (query.prepare(QString("SELECT accno, fullname, address, telephone FROM user_reg_elec WHERE username = :username AND password = :password"))) {
//Add bindings
query.bindValue(":username","username");
query.bindValue(":password","password");
if(query.exec()) {
while(query.next()) {
ui ->dislayaccountnumber ->setText(query.value(0).toString());
ui ->displayname ->setText(query.value(1).toString());
ui ->displayaddress ->setText(query.value(2).toString());
ui ->displattelephoneno ->setText(query.value(3).toString());
// ui ->displayamountoebill ->setText(query.value(4).toString());
}
} else {
qDebug() << "Query did not execute due to: " << query.lastError().text();
QMessageBox::information(this, "Query did not execute", "Not successful executing the query");
}
} else {
qDebug() << "Query not prepared due to the following error: " << query.lastError().text();
}
} else {
qDebug() << "Database not opened due to: " << database.lastError().text();
QMessageBox::information(this, "Database not open", "Not opened successfully");
}
database.close();
}
QSqlDatabase::removeDatabase("MyConnect");
}
When your application accepts a user (using a login phase), you should save the user id in your application and pass it using a set function member or even as parameter in your ctor of UserDetails. So, when your application displays the user details, you can create a SQL query referring directly the user throw her/him id, avoiding use any other field in where clausule.
A suggestion:
class Userdetails
{
private:
std::string _user;
...
setUser(const std::string & user){
_user = user;
}
...
}
But, I strongly recommend, for security reasons, the password should not be used, you already log in your user, and the user id IS unique.

I'm trying to retrieve data from qSQL database

But it does not get the data from the database. I'm do not want to retrieve the data to the table and want to display data in particular line edit. What is the error? Is there any modification?
This is the Code that I'm using:
void Userdetails::on_pushButton_4_clicked()
{
delete ui;
// database connection
database = QSqlDatabase::addDatabase("QMYSQL");
database.setHostName("localhost");
database.setUserName("root");
database.setPassword("");
database.setDatabaseName("electricity");
if(database.open()) {
QSqlQuery qry;
QSqlQuery query(QSqlDatabase::database("MyConnect"));
query.prepare(QString("SELECT accno, fullname, address, telephone FROM user_reg_elec WHERE username = :username AND password = :password"));
if(query.exec()) {
query.exec();
while(query.next()) {
ui ->dislayaccountnumber ->setText(query.value(0).toString());
ui ->displayname ->setText(query.value(3).toString());
ui ->displayaddress ->setText(query.value(4).toString());
ui ->displattelephoneno ->setText(query.value(5).toString());
// ui ->displayamountoebill ->setText(query.value(6).toString());
}
} else {
QMessageBox::information(this, "Query did not execute", "Not successful executing the query");
}
} else {
QMessageBox::information(this, "Database not open", "Not opened successfully");
}
database.close();
}
This code has four main issues:
You have deleted the ui at the beginning of your code. Hence, the first line that calls ui-> will crash the program.
You have defined your query improperly. Also, your way to select a name for your connection is not right.
You have executed your query twice (one is enough).
You've not bind values for username and password.
Please use the following:
void Userdetails::on_pushButton_4_clicked() {
{
// database connection
QSqlDatabase database;
database = QSqlDatabase::addDatabase("QMYSQL","MyConnect");
database.setHostName("localhost");
database.setUserName("root");
database.setPassword("");
database.setDatabaseName("electricity");
if(database.open()) {
QSqlQuery query(database);
if (query.prepare(QString("SELECT accno, fullname, address, telephone FROM user_reg_elec WHERE username = :username AND password = :password"))) {
//Add bindings
query.bindValue(":username","your user name");
query.bindValue(":password","your password");
if(query.exec()) {
while(query.next()) {
ui ->dislayaccountnumber ->setText(query.value(0).toString());
ui ->displayname ->setText(query.value(1).toString());
ui ->displayaddress ->setText(query.value(2).toString());
ui ->displattelephoneno ->setText(query.value(3).toString());
// ui ->displayamountoebill ->setText(query.value(4).toString());
}
} else {
qDebug() << "Query did not execute due to: " << query.lastError().text();
QMessageBox::information(this, "Query did not execute", "Not successful executing the query");
}
} else {
qDebug() << "Query not prepared due to the following error: " << query.lastError().text();
}
} else {
qDebug() << "Database not opened due to: " << database.lastError().text();
QMessageBox::information(this, "Database not open", "Not opened successfully");
}
database.close();
}
QSqlDatabase::removeDatabase("MyConnect");
}
Please add #include <QSqlError> to the top, if you have not already included this library.
Only to the questionary:
To show details in a new window, after you got the target user's information from your database, you can create a new dialog (window) and show the results in it as follows:
//Create a new dialog
QDialog *dialog = new QDialog;
//Add some elements to the dialog
QLabel *accountNumber = new QLabel("Account number: " + query.value(0).toString());
QLabel *name = new QLabel("Name: " + query.value(1).toString());
QLabel *address = new QLabel("Address: " + query.value(2).toString());
QLabel *phoneNumber = new QLabel("Phone number: " + query.value(3).toString());
QVBoxLayout *lay = new QVBoxLayout;
lay->addWidget(accountNumber);
lay->addWidget(name);
lay->addWidget(address);
lay->addWidget(phoneNumber);
dialog->setLayout(lay);
//Show the dialog
dialog->open();

qt5 selecting data from mysql database

I want to make a login system, I have mysql database I want to login according to my mysql database username and password but it is not working I think I have problem with my code please check the code
void MainWindow::on_loginBtn_clicked()
{
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QMYSQL", "MyConnect");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("qtregister");
QString username = ui->loginEdit->text();
QString password = ui->loginPassword->text();
if(db.open()) {
QSqlQuery query(QSqlDatabase::database("MyConnect"));
query.prepare(QString("SELECT username and password from users where username = :username AND password = :password"));
query.bindValue(":username", username);
query.bindValue(":password", password);
if(!query.exec()) {
QMessageBox::information(this, "Failed", "Failed To Login");
}else {
QMessageBox::information(this, "Success", "Login Success");
}
}
else {
QMessageBox::information(this, "Not Connected", "Not Conneced Success");
}
}
UPDATED
There was an error in your query (Sometimes you need to debug your query in another environment that Qt like mysql clis or online ones http://sqlfiddle.com)
SELECT username,password from users where username = :username AND password = :password
instead of
SELECT username and password from users where username = :username AND password = :password
Corrected Answer
void MainWindow::on_loginBtn_clicked() {
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QMYSQL", "MyConnect");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("qtregister");
QString username = ui - > loginEdit - > text();
QString password = ui - > loginPassword - > text();
if (db.open()) {
QSqlQuery query(db);
query.prepare(QString("SELECT username , password from users where username = :username AND password = :password"));
query.bindValue(":username", username);
query.bindValue(":password", password);
if (!query.exec()) {
QMessageBox::information(this, "Failed", "Error in executing query");
} else {
while (query.next()) {
QString usernameFromDB = query.value(0).toString();
QString passwordFromDB = query.value(1).toString();
qDebug() << usernameFromDB << passwordFromDB;
if (usernameFromDB == username && passwordFromDB == password)
QMessageBox::information(this, "Success", "Login Success");
else
QMessageBox::information(this, "Failed", "Username or password error");
}
}
} else {
QMessageBox::information(this, "Not Connected", "Not Conneced Success");
}
}

Add recipients to existing recipient list with C# code - Sitecore Email campaign manager

I want to add new recipients to existing recipientlist using code. I tried with below code but it didnt work.
TargetAudience recipientList = Factory.GetTargetAudience("RecipientListId");
if ((recipientList != null))
{
Contact contact = //I dont know how to create object for this, because it is protected class
contact.Profile.Email = "my Email";
contact.Profile.Name = "My Name";
contact.Profile.FullName = "Full Name";
recipientList.Subscribers.Add(contact);
}
Please help me to acheive this,
Thanks in advance
You can get a contact from the username of the user.
This method gets the contact by email address and contains code to get the username from the email address.
public Contact GetContact(string email)
{
// managerRoot is the top level ECM item
ManagerRoot managerRootFromId = Factory.GetManagerRootFromID(managerRoot.ID.ToString());
var username = Util.AddressToUserName(email);
string commonDomain = managerRootFromId.Settings.CommonDomain;
string fullName = commonDomain + "\\" + Util.AddressToUserName(username);
if (User.Exists(fullName))
{
return Contact.FromName(fullName);
}
return null;
}
You should then be able to add the Contact to the subscription list.
Or once you have the Contact you can set the profile values and use the subscribe method.
contact.InnerUser.Profile["Fullname"] = string.Format("{0} {1}",person.Firstname,person.Surname);
contact.Subscribe(subscriptionLists);
You can also add ECM users by using the following code supplying the email address as the localname.
protected static Contact CreateAnonymousECMUser(string localName, ManagerRoot root)
{
Contact contact = (Contact)null;
if (root != null && !string.IsNullOrEmpty(localName))
{
string commonDomain = root.Settings.CommonDomain;
Assert.IsNotNullOrEmpty(commonDomain, EcmTexts.Localize("The Common Domain setting is not set.", new object[0]));
string str = commonDomain + "\\" + Util.AddressToUserName(localName);
while (User.Exists(str))
str = str + "_";
string password = new PasswordGenerator()
{
MinimumCharacters = 14
}.Generate();
System.Web.Security.Membership.CreateUser(str, password, localName);
contact = Contact.FromName(str);
contact.Profile.ProfileItemId = root.Settings.SubscriberProfile;
contact.Profile.Save();
}
return contact;
}

Qt SQL prepare fails

I'm trying to run this Qt code
QString serverName = "localhost";
QString dbName = "zfserver";
QString userName = "root";
QString passWord = "123456";
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setConnectOptions();
db.setHostName(serverName);
db.setDatabaseName(dbName);
db.setUserName(userName);
db.setPassword(passWord);
if(db.open())
{
QSqlQuery query;
query.prepare("INSERT INTO account (name, email, password, type) "
"VALUES (:name, :email, :password, :type)");
query.bindValue(":name", "atef");
query.bindValue(":email", "asfasf#gfasga.com");
query.bindValue(":password", "123");
query.bindValue(":type", "2");
if (query.exec())
{
qDebug() << "OK";
} else {
qDebug() << "Error" << query.lastError().text();
}
db.close();
}
But I'm getting this error
Error "Using unsupported buffer type: 1701601889 (parameter: 1) QMYSQL3: Unable
to bind value"
If I change the query without the bindValue it works. Is there a way to solve this?
Try to rebuild the SQL driver .
QMYSQL3 seems to be old.