修改了insert_base,增加了注册页面和修改login页面

This commit is contained in:
chang 2023-10-25 17:27:33 +08:00
parent a50020d7a3
commit fc2fdc3b6b
3 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,115 @@
#include <cjson/cJSON.h>
#include <stdio.h>
// #include "cJSON.h"
#include <stdlib.h>
#include "sqlite3.h"
// cJSON *result_json; // 用于存储查询结果
// sqlite3的回调函数
// sqlite 每查到一条记录,就调用一次这个回调
int sql_search_callback(void* NotUsed, int argc, char** argv, char** azColName);
int main(int argc, char const* argv[]) {
// result_json = cJSON_CreateObject(); // 创建 JSON 对象
// 连接到 SQLite 数据库
sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象
char* zErrMsg = 0; // 用于存储错误信息
int rc; // 用于存储函数返回值
rc = sqlite3_open(
"../sql_base/green_house.db",
&db); // 打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
fprintf(stderr, "ok Opened database successfully\n");
// 获取前端传递的 JSON 数据
char request_data[128] = "";
fgets(request_data, 128, stdin); // 从标准输入中读取一行数据
// 解析 JSON 数据
cJSON* json_buf = cJSON_Parse(request_data); // 解析 JSON 数据
if (json_buf == NULL) {
fprintf(stderr, "Error parsing JSON data\n");
sqlite3_close(db);
return 1;
}
// 获取用户名和密码
cJSON* user = cJSON_GetObjectItemCaseSensitive(json_buf, "name");
cJSON* password = cJSON_GetObjectItemCaseSensitive(json_buf, "password");
if (user == NULL || password == NULL || !cJSON_IsString(user) ||
!cJSON_IsString(password)) { // 是否是非法输入
fprintf(stderr,
"Error getting username and/or password from JSON data\n");
cJSON_Delete(json_buf);
sqlite3_close(db);
return 1;
}
// 拿到用户名和密码
char* username_from_frontend = user->valuestring;
char* password_from_frontend = password->valuestring;
printf("username is %s, pwd is %s\n", username_from_frontend,
password_from_frontend);
// 构建查询语句
char query_sql[128] = "";
sprintf(query_sql,
"insert into login_users (username,password) values('%s','%s');",
username_from_frontend, password_from_frontend); // 构建插入语句
printf("query_sql is %s\n", query_sql);
// 执行查询语句
rc = sqlite3_exec(db, query_sql, NULL, NULL, &zErrMsg); // 执行查询语句
// 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);
sqlite3_close(db);
return 1;
}
// 释放资源
cJSON_Delete(json_buf);
sqlite3_close(db);
return 0;
}
int sql_search_callback(void* NotUsed,
int argc,
char** argv,
char** azColName) {
// int i;
// for (i = 0; i < argc; i++)
// {
// 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);
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);
return SQLITE_OK;
}

43
html/enroll.html Normal file
View File

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form onsubmit="return login(this);">
<div class="row">
<input name="name" placeholder="请输入账号">
</div>
<div class="row">
<input name="password" type="password" placeholder="请输入密码">
</div>
<div class="row center">
<button>确认</button>
</div>
</div>
</form>
</body>
<script>
function login(form) {
let login_data = {
name: form.name.value.trim(),
password: form.password.value.trim()
};
fetch("../cgi-bin/sqlite_cgi_insert_base.cgi", {
method: "POST",
body: JSON.stringify(login_data),
headers: {
"Content-Type": "application/json;charset=utf-8"
}
})
open("./login.html", "_self");
// window.localStorage.setItem("login_user", JSON.stringify(data));
return false;
}
</script>
</html>

View File

@ -134,6 +134,11 @@
<div class="row center"> <div class="row center">
<button>登录</button> <button>登录</button>
</div> </div>
<div class="row center">
<button>
<a href="enroll.html" target="content_frame1" class="menu_item">注册</a>
</button>
</div>
</form> </form>
</div> </div>
<div></div> <div></div>