It seems like my function is only receiving the first char of a string when being called

80 views Asked by At

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.

2

There are 2 answers

0
arfneto On

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.

this is the function prototype:

   int password_match(const char* a, const char* b) 

password_match() receives 2 pointers to char. No char at all. The pointers can point to NULL, to valid or invalid NULL-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 and a[1] is zero, or the same for b.

about the code

  • loginUser() is just a wrapper around 2 strings in order to test password_match(). Do not write interactive code to test your program. It will only cost you time. Much time. Just pass 2 strings to loginUser and return the user id
  • do not use void f(void): this things will get no data and return nohing so will depend entirely on global external data.
  • code to check user and password in a database is not relevant here yet: you just want to test the arguments coming into password_match()
  • do not mix snake_case and camelCase. In general in C we expect snake_case, but anyway just be consistent.
0
Trey McCloud On

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.