<%pre> #include #include #include #include #include #include "common.h" <%config> dburl; <%session scope="global"> std::string userName; bool isAdmin; <%args> int id; <&header title="Customer Administration"> <{ if (userName.size() == 0) { reply.setHeader("Refresh", "0; url=login"); return HTTP_OK; } if (id == -1) reply.out() << "

New Customer

\n"; else reply.out() << "

Modify customer

\n"; reply.out() << ""; reply.sout() << "<< back to customer list"; reply.out() << "
"; tntdb::Connection conn = tntdb::connect(dburl); std::string submit = qparam.param("submit"); if (submit == "1") { std::string form_name = trim(qparam.param("name")); std::string form_address = trim(qparam.param("address")); std::string form_phone = trim(qparam.param("phone")); std::string form_contact = trim(qparam.param("contact")); if (id == -1) { tntdb::Statement st = conn.prepare("INSERT INTO customer (name,address,phonenr,contactperson) values (:v1, :v2, :v3, :v4)"); st.setString("v1", form_name).setString("v2", form_address).setString("v3", form_phone).setString("v4", form_contact); st.execute(); //tntdb::Value v = conn.selectValue("SELECT lastval()"); //get the auto-generated id //id = v.getInt(); reply.setHeader("Refresh", "0; url=adm_customer_list"); return HTTP_OK; } else { tntdb::Statement st = conn.prepare("UPDATE customer SET name=:v1, address=:v2, phonenr=:v3, contactperson=:v4 WHERE id=:v5"); st.setString("v1", form_name).setString("v2", form_address).setString("v3", form_phone).setString("v4", form_contact).setInt("v5", id); st.execute(); } } std::string name; std::string address; std::string phone; std::string contact; bool showForm = true; if (id != -1) { std::stringstream query; query << "SELECT name,address, phonenr,contactperson FROM customer WHERE id = " << id; tntdb::Result res = conn.select(query.str()); if (res.size() > 0) { name = res[0].getString(0); address = res[0].getString(1); phone = res[0].getString(2); contact = res[0].getString(3); } else { reply.out() << "

Invalid customer ID !

\n"; showForm = false; } } if (showForm) { }>
Name:
Address:
Phone:
Contact:
 

<{ } }> <&footer>