diff --git a/cgi-bin/mqtt_pub_stoc.c b/cgi-bin/mqtt_pub_stoc.c index 4f907d0..e525577 100644 --- a/cgi-bin/mqtt_pub_stoc.c +++ b/cgi-bin/mqtt_pub_stoc.c @@ -25,8 +25,8 @@ int main(int argc, char const *argv[]) mosquitto_lib_init(); // 初始化(固定格式) struct mosquitto *client = mosquitto_new(NULL, true, NULL); // 创建客户端 mosquitto_connect_callback_set(client, connect_callback); // 设置连接回调函数 - mosquitto_connect(client, "localhost", 1883, 60); // 连接服务器(地址,端口,超时时间) -// mosquitto_connect(client, "flykhan.com", 1883, 60); // 连接服务器(地址,端口,超时时间) + // mosquitto_connect(client, "localhost", 1883, 60); // 连接服务器(地址,端口,超时时间) + mosquitto_connect(client, "flykhan.com", 1883, 60); // 连接服务器(地址,端口,超时时间) mosquitto_loop_start(client); // 开启客户端线程 while (isConnected == 0) ; // 等待连接成功(当连接成功后,isConnected会被置为1,然后跳出循环) diff --git a/cgi-bin/sqlite_cgi_base.c b/cgi-bin/sqlite_cgi_base.c index bbb8793..29c3d16 100644 --- a/cgi-bin/sqlite_cgi_base.c +++ b/cgi-bin/sqlite_cgi_base.c @@ -5,6 +5,7 @@ // cJSON *result_json; // 用于存储查询结果 char login_user_name[32] = ""; +int sql_search_callback_result_number = 0; // 用于存储查询结果的条数(默认为0) // sqlite3的回调函数 // sqlite 每查到一条记录,就调用一次这个回调 @@ -79,6 +80,7 @@ int main(int argc, char const *argv[]) rc = sqlite3_exec(db, query_sql, sql_search_callback, 0, &zErrMsg); // 执行查询语句 if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); cJSON_Delete(json_buf); @@ -86,6 +88,27 @@ int main(int argc, char const *argv[]) return 1; } + // 检查查询结果是否为0 + if (sql_search_callback_result_number == 0) + { + cJSON *result_json = cJSON_CreateObject(); + cJSON_AddNumberToObject(result_json, "code", 1); + 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(json_buf); + sqlite3_close(db); + return 1; + } + + sql_search_callback_result_number = 0; // 重置为0 // 释放资源 cJSON_Delete(json_buf); sqlite3_close(db); @@ -104,16 +127,11 @@ int sql_search_callback(void *NotUsed, // printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); // } cJSON *result_json = cJSON_CreateObject(); - if (argc == 0) - { - cJSON_AddNumberToObject(result_json, "code", 1); - cJSON_AddStringToObject(result_json, "msg", "用户名或密码错误"); - } - else - { - cJSON_AddNumberToObject(result_json, "code", 0); - cJSON_AddStringToObject(result_json, "login_user_name", login_user_name); - } + sql_search_callback_result_number++; + + cJSON_AddNumberToObject(result_json, "code", 0); + cJSON_AddStringToObject(result_json, "login_user_name", login_user_name); + char *result_json_string = cJSON_Print(result_json); // 输出 JSON 结果 diff --git a/html/enroll.html b/html/enroll.html index cb41002..cd2d64c 100644 --- a/html/enroll.html +++ b/html/enroll.html @@ -4,7 +4,54 @@
-