/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <mysql.h>

static char *parse(char *line, char *marker)
{
    char *p = strstr(line, marker);
    if(p == NULL)
    {
        return NULL;
    }
    else
    {
        p = p + strlen(marker);
        char *res = malloc(1024);
        char *p2 = p;
        while(*p2 && *p2 != '&') p2++;
        memcpy(res, p, p2 - p);
        res[p2 - p] = 0;
        for(int i = 0; i < strlen(res); i++) if(res[i] == '+') res[i] = ' ';
        return res;
    }
}

int main(int argc, char *argv[])
{
    char *f1 = NULL;
    char *f2 = NULL;
    char *f3 = NULL;
    char line[1024];
    if(fgets(line, sizeof(line), stdin))
    {
        f1 = parse(line, "F1=");
        f2 = parse(line, "F2=");
        f3 = parse(line, "F3=");
    }
    printf("Status: 200 OK\n");
    printf("Content-Type: text/html\n");
    printf("\n");
    printf("<html>\n");
    printf("<head>\n");
    printf("<title>CGI - C</title>\n");
    printf("</head>\n");
    printf("<body>\n");
    printf("<h1>CGI - C</h1>\n");
    printf("<h2>Show data:</h2>\n");
    printf("<table border=\"1\">\n");
    printf("    <tr>\n");
    printf("        <th>F1</th>\n");
    printf("        <th>F2</th>\n");
    printf("        <th>F3</th>\n");
    printf("    </tr>\n");
    int stat;
    MYSQL *con = mysql_init(NULL);
    con = mysql_real_connect(con, "localhost", "root", "hemmeligt", "Test", 3306, NULL, 0);
    if(strcmp(getenv("REQUEST_METHOD"), "POST") == 0 && f1 != NULL && strlen(f1) > 0)
    {
        MYSQL_STMT *stmt = mysql_stmt_init(con);
        char *sqlstr = "INSERT INTO t1demo VALUES(?,?,?)";
        stat = mysql_stmt_prepare(stmt, sqlstr, strlen(sqlstr));
        int xf1 = atoi(f1);
        MYSQL_BIND in[3];
        memset(in, 0, sizeof(in));
        in[0].buffer_type = MYSQL_TYPE_LONG;
        in[0].buffer = &xf1;
        in[0].buffer_length = sizeof(long);
        in[0].is_null= 0;
        in[1].buffer_type = MYSQL_TYPE_STRING;
        in[1].buffer = f2;
        in[1].buffer_length = strlen(f2);
        in[2].buffer_type = MYSQL_TYPE_STRING;
        in[2].buffer = f3;
        in[2].buffer_length = strlen(f3);
        stat = mysql_stmt_bind_param(stmt, in);
        stat = mysql_stmt_execute(stmt);
    }
    stat = mysql_query(con, "SELECT f1,f2,f3 FROM t1demo");
    MYSQL_RES *result = mysql_store_result(con);
    MYSQL_ROW row;
    while(row = mysql_fetch_row(result))
    {
        printf("    <tr>\n");
        printf("        <td>%s</td>\n", row[0]);
        printf("        <td>%s</td>\n", row[1]);
        printf("        <td>%s</td>\n", row[2]);
        printf("    </tr>\n");
    }
    mysql_free_result(result);
    mysql_close(con);
    printf("</table>\n");
    printf("<h2>Add data:</h2>\n");
    printf("<form method=\"POST\" name=\"AddForm\">\n");
    printf("F1: <input type=\"text\" name=\"F1\">\n");
    printf("<br>\n");
    printf("F2: <input type=\"text\" name=\"F2\">\n");
    printf("<br>\n");
    printf("F3: <select name=\"F3\">\n");
    printf("        <option></option>\n");
    printf("        <option>Verified</option>\n");
    printf("        <option>Not verified</option>\n");
    printf("    </select>\n");
    printf("<br>\n");
    printf("<input type=\"submit\" name=\"Save\" value=\"Add\">\n");
    printf("</form>\n");
    printf("</body>\n");
    printf("</html>\n");
    return 0;
}
