再再次提交

This commit is contained in:
wang_chengh 2023-09-20 16:10:47 +08:00
parent 0f676ee934
commit cdf2350d9f
2 changed files with 17 additions and 22 deletions

View File

@ -8,23 +8,20 @@
#include <sys/types.h>
#include <unistd.h>
int main()
{
int main() {
// 连接到 MySQL 数据库
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
conn = mysql_init(NULL);
if (conn == 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)
{
NULL, 0) == NULL) {
fprintf(stderr, "mysql_real_connect() failed\n");
mysql_close(conn);
exit(1);
@ -36,8 +33,7 @@ int main()
// 解析 JSON 数据
cJSON *p = cJSON_Parse(request_data);
if (p == NULL)
{
if (p == NULL) {
fprintf(stderr, "Error parsing JSON data\n");
mysql_close(conn);
return 1;
@ -48,8 +44,7 @@ int main()
cJSON *pwd = cJSON_GetObjectItemCaseSensitive(p, "pwd");
if (user == NULL || pwd == NULL || !cJSON_IsString(user) ||
!cJSON_IsString(pwd))
{
!cJSON_IsString(pwd)) {
fprintf(stderr,
"Error getting username and/or password from JSON data\n");
cJSON_Delete(p);
@ -65,8 +60,7 @@ int main()
sprintf(query, "SELECT * FROM users WHERE username='%s' AND password='%s'",
username_from_frontend, password_from_frontend);
if (mysql_query(conn, query))
{
if (mysql_query(conn, query)) {
fprintf(stderr, "SELECT query failed. Error: %s\n", mysql_error(conn));
cJSON_Delete(p);
mysql_close(conn);
@ -74,8 +68,7 @@ int main()
}
res = mysql_store_result(conn);
if (res == NULL)
{
if (res == NULL) {
fprintf(stderr, "mysql_store_result() failed\n");
cJSON_Delete(p);
mysql_close(conn);
@ -84,13 +77,10 @@ int main()
cJSON *result_json = cJSON_CreateObject();
if ((row = mysql_fetch_row(res)))
{
if ((row = mysql_fetch_row(res))) {
cJSON_AddNumberToObject(result_json, "code", 0);
cJSON_AddStringToObject(result_json, "nickname", "admin");
}
else
{
} else {
cJSON_AddNumberToObject(result_json, "code", 1);
cJSON_AddStringToObject(result_json, "msg", "用户名或口令错误");
}
@ -110,3 +100,5 @@ int main()
return 0;
}
// 注释

View File

@ -2,7 +2,7 @@
CREATE database if NOT EXISTS router;
USE router;
-- 创建用户登录密码表
CREATE TABLE users (
CREATE TABLE if not exists users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
@ -10,10 +10,13 @@ CREATE TABLE users (
INSERT INTO users (username, password)
VALUES ('admin', 'admin');
-- 创建ARP映射表
CREATE TABLE ip_mac(
CREATE TABLE if not exists ip_mac(
id INT AUTO_INCREMENT PRIMARY KEY,
ip VARCHAR(50) NOT NULL,
mac VARCHAR(50) NOT NULL
);
-- 创建防火墙表
CREATE TABLE ip_fw(ip VARCHAR(50) NOT NULL);
CREATE TABLE if not exists ip_fw(
id INT AUTO_INCREMENT PRIMARY KEY,
ip VARCHAR(50) NOT NULL
);