From dba3d23f8df4ba3a288c1e634fc5f2efc60bc24b Mon Sep 17 00:00:00 2001 From: wang_chengh <13383929+wang_chengh@user.noreply.gitee.com> Date: Tue, 19 Sep 2023 17:18:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8c=E4=BB=A3=E7=A0=81=E4=B8=AD=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E5=88=9B=E5=BB=BA=E6=95=B0=E6=8D=AE=E5=BA=93=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E4=B8=94=E5=81=9A=E4=BA=86cmp=EF=BC=8C=E8=BF=94?= =?UTF-8?q?=E5=9B=9Ejson=E7=B1=BB=E5=9E=8B=E7=9A=84code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- www/cgi-bin/c_mysql_user&pwd.c | 118 +++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 www/cgi-bin/c_mysql_user&pwd.c diff --git a/www/cgi-bin/c_mysql_user&pwd.c b/www/cgi-bin/c_mysql_user&pwd.c new file mode 100644 index 0000000..9ac210a --- /dev/null +++ b/www/cgi-bin/c_mysql_user&pwd.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() { + // 连接到 MySQL 数据库 + MYSQL *conn; + MYSQL_RES *res; + MYSQL_ROW row; + + conn = mysql_init(NULL); + if (conn == NULL) { + fprintf(stderr, "mysql_init() failed\n"); + exit(1); + } + + if (mysql_real_connect(conn, "localhost", "root", "root", "router", 3306, + NULL, 0) == NULL) { + fprintf(stderr, "mysql_real_connect() failed\n"); + mysql_close(conn); + exit(1); + } + + if (mysql_query(conn, + "CREATE TABLE IF NOT EXISTS users(id INT AUTO_INCREMENT " + "PRIMARY KEY " + "username VARCHAR(50) NOT NULL, password VARCHAR(255) NOT " + "NULL);")) { + fprintf(stderr, "CREATE TABLE query failed. Error: %s\n", + mysql_error(conn)); + mysql_close(conn); + exit(1); + } + + // 获取前端传递的 JSON 数据 + char request_data[128] = ""; + fgets(request_data, 128, stdin); + + // 解析 JSON 数据 + cJSON *p = cJSON_Parse(request_data); + if (p == NULL) { + fprintf(stderr, "Error parsing JSON data\n"); + mysql_close(conn); + return 1; + } + + // 获取用户名和密码 + cJSON *user = cJSON_GetObjectItemCaseSensitive(p, "user"); + cJSON *pwd = cJSON_GetObjectItemCaseSensitive(p, "pwd"); + + if (user == NULL || pwd == NULL || !cJSON_IsString(user) || + !cJSON_IsString(pwd)) { + fprintf(stderr, + "Error getting username and/or password from JSON data\n"); + cJSON_Delete(p); + mysql_close(conn); + return 1; + } + + char *username_from_frontend = user->valuestring; + char *password_from_frontend = pwd->valuestring; + + // 构建查询语句 + char query[1000]; + sprintf(query, "SELECT * FROM users WHERE username='%s' AND password='%s'", + username_from_frontend, password_from_frontend); + + if (mysql_query(conn, query)) { + fprintf(stderr, "SELECT query failed. Error: %s\n", mysql_error(conn)); + cJSON_Delete(p); + mysql_close(conn); + return 1; + } + + res = mysql_store_result(conn); + if (res == NULL) { + fprintf(stderr, "mysql_store_result() failed\n"); + cJSON_Delete(p); + mysql_close(conn); + return 1; + } + + cJSON *result_json = cJSON_CreateObject(); + + if ((row = mysql_fetch_row(res))) { + cJSON_AddNumberToObject(result_json, "code", 0); + } else { + cJSON_AddNumberToObject(result_json, "code", 1); + cJSON_AddStringToObject(result_json, "msg", "用户名或口令错误"); + } + + /* if ((row = mysql_fetch_row(res))) { + cJSON_AddStringToObject(result_json, "code", 0); + } else { + cJSON_AddStringToObject(result_json, "msg", "登录失败"); + } */ + + char *result_json_string = cJSON_Print(result_json); + + // 输出 JSON 结果 + printf("content-type: application/json;charset=utf-8\r\n"); + printf("\r\n"); + printf("%s\n", result_json_string); + + free(result_json_string); + cJSON_Delete(result_json); + cJSON_Delete(p); + mysql_free_result(res); + mysql_close(conn); + + return 0; +}