// dynanew #include #include #include #include #include #include struct Customer { string mobileNo, name, passWord, birth, address, city, eMail; float amountOutstanding; }; int isNumberAlreadyUsed(string c) { char *qbuff; qbuff = new char[255]; MYSQL mysql; MYSQL_RES *res; MYSQL_ROW row; char mobileNo[11]; int len = c.length(); int numRows = 0; if(len == 0) { // cout << endl << "return 10" << endl; return 10; } if(len < 11) { // cout << endl << "return 15" << endl; return 15; } for(int ctr = 0; ctr < len; ctr++) { mobileNo[ctr] = c.at(ctr); } mobileNo[11] = '\0'; for(int ctr = 0; ctr < 11; ctr++) { if(mobileNo[ctr] < '0' || mobileNo[ctr] > '9') { // cout << endl << "return 17" << endl; return 17; } } // cout << endl << "mobileNo = " << mobileNo << endl; if(!mysql_init(&mysql)) cout << endl << "Can not initialize MYSQL" << mysql_error(&mysql) << endl; if(!mysql_real_connect(&mysql, NULL, "root", NULL, "Dyna", 0, 0, 0)) cout << endl << "Can not connect to MYSQL" << mysql_error(&mysql) << endl; sprintf(qbuff, "select * from Customer where cMobileNo = '%s'", mobileNo); if(!mysql_query(&mysql, qbuff)) cout << endl << mysql_error(&mysql) << endl; res = mysql_store_result(&mysql); // cout << endl << "after res" << endl; // cout << endl << "affected rows = " << mysql_affected_rows(&mysql); numRows = mysql_num_rows(res); // cout << endl << "numRows = " << numRows << endl; if(numRows == 1) { // cout << endl << "return 20" << endl; return 20; } else { // cout << endl << "return 30" << endl; return 30; } mysql_close(&mysql); delete [] qbuff; } class WebProcess { string input, dest[7], errorMessage; Customer cust; public: void get() { input.assign(getenv("QUERY_STRING")); cout << "Content-type: text/html" << endl << endl; } void replaceAll(const string &findstr, const string &replstr) { int found = input.find(findstr); while(found != string::npos) { input.replace(found, findstr.length(), replstr); found = input.find(findstr); } } void urlDecode() { replaceAll("%2C", ","); // replaceAll("%27", "\''"); replaceAll("%40", "@"); replaceAll("+", " "); split(); } void split() { int count = 0; char *p; p = strtok(input.begin(), "&"); while(p != NULL) { dest[count] = p; count++; p = strtok(0, "&"); } for(int counter = 0; counter != count; counter++) { dest[counter].erase(0, dest[counter].find("=") + 1); cust.mobileNo = dest[0]; cust.name = dest[1]; cust.passWord = dest[2]; cust.birth = dest[3]; cust.address = dest[4]; cust.city = dest[5]; cust.eMail = dest[6]; // cust.amountOutstanding = dest[7]; } } void checkRecord() { int msg = 0; msg = isNumberAlreadyUsed(cust.mobileNo); if(msg == 10) { displayStart("Mobile phone number cannot be empty."); } else { if(msg == 15) { displayStart("Mobile phone should be 11 characters."); } else { if(msg == 17) { displayStart("Mobile phone number should all be numbers."); } else { if(msg == 20) { displayStart("Mobile phone number already used."); } else { displayBlankForm(); } } } } } void displayStart(string msg) { cout << ""; cout << "

Dyna Telecommunications Inc.

"; cout << "

" << msg << "

"; cout << endl << "
"; cout << "Welcome to the Dyna Telecommunications web Site. To begin the transaction, please choose from the two options given below."; cout << "

New Customer

"; cout << "
"; cout << "
          Mobile Telephone Number: 
"; cout << "
"; cout << endl << "
"; cout << "

Existing Customer

"; cout << "
"; cout << "
          Mobile Telephone Number: 
Password:
"; cout << "
"; cout << ""; } void displayBlankForm() { // cout << "Content-type: text/html\n\n"; cout << ""; cout << "

Dyna Telecommunications Inc.

"; cout << "Please fill out the applicaton form given below. Birth date should be entered as yyyy-mm-dd."; cout << "

Application Form

"; cout << "
"; cout << "
        Mobile Telephone Number: 
Customer Name:
Password:
Date of Birth:   yyyy-mm-dd
Address:
City:
Email:
"; cout << "
"; cout << ""; } }; int main() { WebProcess web; web.get(); web.urlDecode(); web.split(); web.checkRecord(); return 0; }