I am building a basic registration feature for a school project and keep getting an error when I try to check the username and password from a database. The idea is to have a user type in their login info and then have a function receive that information and compare it with the user info from the database. if it matches it will retrieve the user id from the table and attach it to a variable for later use. I am having an issue now where it seems like when I pass in the user input into the password match function, the function only receives the first char of the input. That’s what the debugger shows. I may be reading the debugger wrong but I am not sure here is the first function
void loginUser(){
char email[100];
char password[50];
// Input login details
printf("Enter email: ");
fgets(email, sizeof(email), stdin);
email[strcspn(email, "n")] = ''; // Remove newline character
printf("Enter password: ");
fgets(password, sizeof(password), stdin);
password[strcspn(password, "n")] = ''; // Remove newline character
printf("Email: %sn", email);
printf("Email: %sn", password);
// Check email and password against database
if(password_match(email, password)){
printf("Success");
};
// If authenticated, proceed to main menu
// Else, show error message and redirect to login
};
and this is the function that gets the username and password from mysql database
int password_match(const char *email, const char *password) {
// printf("Email: %sn", email);
// printf("Email: %sn", password);
MYSQL_RES *result;
MYSQL_ROW row;
char query[512];
snprintf(query, sizeof(query), "SELECT consumerID FROM CurrentConsumer WHERE email='%s' AND password='%s'", email, password);
if (mysql_query(con, query)) {
fprintf(stderr, "Error querying database: %sn", mysql_error(con));
return 0; // Failure
}
result = mysql_store_result(con);
if (result == NULL) {
fprintf(stderr, "Error storing result: %sn", mysql_error(con));
return 0; // Failure
}
row = mysql_fetch_row(result);
if (row == NULL) {
mysql_free_result(result);
return 0; // No match
}
// Print the consumer ID
printf("Consumer ID: %sn", row[0]);
mysql_free_result(result);
return 1; // Match
}
I tried using both pass by value and pass by reference but cant get it to work. It’s weird because I have another function that inserts data into the database just fine. I used that to build this function but cant get it to work.
2
Answers
Thank you all for the help both with suggestions and editing the post. The issue was that I didn't take time to review and look at the obvious. I forgot to include a function to start the actual database. so the queries had no place to go. I should have just waited and looked at the problem under fresh eyes instead of trying to solve it at 3:30 in the morning. My bad for taking up your time. Next I can worry about sql injection to make sure the system is secure. Thank you all again.
this is the function prototype:
password_match()
receives 2 pointers tochar
. Nochar
at all. The pointers can point toNULL
, to valid or invalidNULL
-terminated things, but are just pointers to some addresses.The only way for it to receive a single letter string is that
a[0]
is a letter anda[1]
is zero, or the same forb
.about the code
loginUser()
is just a wrapper around 2 strings in order to testpassword_match()
. Do not write interactive code to test your program. It will only cost you time. Much time. Just pass 2 strings tologinUser
and return the user idvoid f(void)
: this things will get no data and return nohing so will depend entirely on global external data.password_match()
C
we expect snake_case, but anyway just be consistent.