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")] = '\0'; // Remove newline character
printf("Enter password: ");
fgets(password, sizeof(password), stdin);
password[strcspn(password, "\n")] = '\0'; // Remove newline character
printf("Email: %s\n", email);
printf("Email: %s\n", 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: %s\n", email);
// printf("Email: %s\n", 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: %s\n", mysql_error(con));
return 0; // Failure
}
result = mysql_store_result(con);
if (result == NULL) {
fprintf(stderr, "Error storing result: %s\n", 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: %s\n", 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.
this is the function prototype:
password_match()receives 2 pointers tochar. Nocharat 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 tologinUserand 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()Cwe expect snake_case, but anyway just be consistent.