(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
[inherit('pmysqldir:pmysql', 'pmysqldir:common', 'pmysqldir:mysql')]
program add(input, output);

const
   EXTRA = chr(13) + chr(10);

var
   con : mysql_ptr;
   stmt : mysql_stmt_ptr;
   stat : integer;
   inparam : array[1..2] of mysql_bind;
   f : text;
   line : varying [1000] of char;
   ix1, ix2 : integer;
   form_f1, form_f2 : varying [255] of char;
   f1 : integer;
   f2 : pstr;

begin
   open(f, 'apache$input', old);
   reset(f);
   readln(f, line);
   close(f);
   ix1 := index(line, 'f1=') + 3;
   ix2 := index(line + '&', '&');
   form_f1 := substr(line, ix1, ix2 - ix1);
   readv(form_f1, f1);
   line := substr(line, ix2 + 1, line.length - ix2);
   ix1 := index(line, 'f2=') + 3;
   ix2 := index(line + '&', '&');
   form_f2 := substr(line, ix1, ix2 - ix1);
   f2 := form_f2;
   con := pmysql_init;
   con := pmysql_real_connect(con, 'localhost', 'root', '', 'test');
   stmt := pmysql_stmt_init(con);
   stat := pmysql_stmt_prepare(stmt, 'INSERT INTO t1 VALUES(?, ?)');
   pmysql_init_bind_long(inparam[1], f1);
   pmysql_init_bind_string_in(inparam[2], f2);
   stat := pmysql_stmt_bind_param(stmt, inparam);
   stat := pmysql_stmt_execute(stmt);
   pmysql_stmt_free_result(stmt);
   pmysql_close(con);
   writeln('Location: http://arne1.vajhoej.dk/cgi-bin/show', EXTRA);
   writeln(EXTRA);
end.
