// dynainsert #include #include #include #include #include #include struct Customer { string mobileNo, name, passWord, birth, address, city, eMail; // float amountOutstanding; }; void updateDB(Customer &c) { // cout << endl << "mow in updateDB" << endl; char *qbuff; qbuff = new char[1024]; MYSQL mysql; MYSQL_RES *res; MYSQL_ROW row; char mobileNo[11], name[25], passWord[25], birth[10], address[50], city[25], eMail[20]; float amount = 0.0f; int len; 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; // cout << endl << "start of convert" << endl; len = c.mobileNo.length(); for(int ctr = 0; ctr < len; ctr++) { mobileNo[ctr] = c.mobileNo.at(ctr); } mobileNo[len] = '\0'; // cout << endl << "mobileNo = " << mobileNo << endl; len = c.name.length(); for(int ctr = 0; ctr < len; ctr++) { name[ctr] = c.name.at(ctr); } name[len] = '\0'; // cout << endl << "name = " << name << endl; len = c.passWord.length(); for(int ctr = 0; ctr < len; ctr++) { passWord[ctr] = c.passWord.at(ctr); } passWord[len] = '\0'; // cout << endl << "passWord = " << passWord << endl; len = c.birth.length(); for(int ctr = 0; ctr < len; ctr++) { birth[ctr] = c.birth.at(ctr); } birth[len] = '\0'; // cout << endl << "birth = " << birth << endl; len = c.address.length(); for(int ctr = 0; ctr < len; ctr++) { address[ctr] = c.address.at(ctr); } address[len] = '\0'; // cout << endl << "address = " << address << endl; len = c.city.length(); for(int ctr = 0; ctr < len; ctr++) { city[ctr] = c.city.at(ctr); } city[len] = '\0'; // cout << endl << "city = " << city << endl; len = c.eMail.length(); for(int ctr = 0; ctr < len; ctr++) { eMail[ctr] = c.eMail.at(ctr); } eMail[len] = '\0'; // cout << endl << "eMail = " << eMail << endl; // cout << endl << "assemble query" << endl; sprintf(qbuff, "insert into Customer values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f')", mobileNo, name, passWord, birth, address, city, eMail, amount); // cout << endl << "before send query" << endl; if(!mysql_query(&mysql, qbuff)) cout << endl << mysql_error(&mysql) << endl; 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]; } } bool isMobileNoValid(string m) { if(m == "") { errorMessage = "Mobile Phone Number must not be null."; return false; } for(int ctr = 0; ctr < 11; ctr++) { if(m[ctr] < '0' || m[ctr] > '9') { errorMessage = "Mobile Phone Number Invalid."; return false; } else { continue; } return true; } } bool isNameNull(string n) { if(n == "") { errorMessage = "Name must not be null."; return false; } else { return true; } } bool isPassWordNull(string p) { if(p == "") { errorMessage = "Password must not be null."; return false; } else { for(int ctr = 0; ctr < p.length(); ctr++) { if(p[ctr] < '0' || p[ctr] > '9') { if(p[ctr] < 'a' || p[ctr] > 'z') { if(p[ctr] < 'A' || p[ctr] > 'Z') { errorMessage = "Password invalid."; return false; } } } } } return true; } void InvalidDateMessage() { errorMessage = "Date of Birth invalid."; } bool isBirthValid(string d) { if(d == "") { errorMessage = "Date of Birth must not be null."; return false; } int dlen = d.length(); if(dlen < 10) { InvalidDateMessage(); return false; } int iyear, imonth, iday, ctr; char cyear[4], cmonth[2], cday[2]; for(ctr = 0; ctr < 4; ctr++) { if(d[ctr] < '0' || d[ctr] > '9') { InvalidDateMessage(); return false; } else { continue; } } if(d[ctr] != '-') { InvalidDateMessage(); return false; } for(ctr = 5; ctr < 7; ctr++) { if(d[ctr] < '0' || d[ctr] > '9') { InvalidDateMessage(); return false; } else { continue; } } if(d[ctr] != '-') { InvalidDateMessage(); return false; } for(ctr = 8; ctr < 10; ctr++) { if(d[ctr] < '0' || d[ctr] > '9') { InvalidDateMessage(); return false; } else { continue; } } for(ctr = 0; ctr < 4; ctr++) { cyear[ctr] = d.at(ctr); } cyear[4] = '\0'; iyear = atoi(cyear); // cout << endl << "year = " << iyear << endl; cmonth[0] = d.at(5); cmonth[1] = d.at(6); cmonth[2] = '\0'; imonth = atoi(cmonth); // cout << endl << "month = " << imonth << endl; cday[0] = d.at(8); cday[1] = d.at(9); cday[2] = '\0'; iday = atoi(cday); // cout << endl << "day = " << iday << endl; if(iyear < 1900 || iyear > 2002) { InvalidDateMessage(); return false; } if(imonth == 0 || imonth > 12 || iday == 0) { InvalidDateMessage(); return false; } if(iyear % 4 == 0 && imonth == 2 && iday > 29) { InvalidDateMessage(); return false; } if(iyear % 4 != 0 && imonth == 2 && iday > 28) { InvalidDateMessage(); return false; } if((imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11) && iday > 30) { InvalidDateMessage(); return false; } if((imonth != 4 && imonth != 6 && imonth != 9 && imonth != 11) && iday > 31) { InvalidDateMessage(); return false; } return true; } bool isAddressNull(string a) { if(a == "") { errorMessage = "Address must not be null."; return false; } else { return true; } } bool isCityNull(string c) { if(c == "") { errorMessage = "City must not be null."; return false; } else { return true; } } bool isEmailNull(string e) { if(e == "") { errorMessage = "Email Address must not be null."; return false; } else { return true; } } bool validateData() { if(isMobileNoValid(cust.mobileNo) == false) { return false; } else { if(isNameNull(cust.name) == false) { return false; } else { if(isPassWordNull(cust.passWord) == false) { return false; } else { if(isBirthValid(cust.birth) == false) { return false; } else { if(isAddressNull(cust.address) == false) { return false; } else { if(isCityNull(cust.city) == false) { return false; } else { if(isEmailNull(cust.eMail) == false) { return false; } else { return true; } } } } } } } } void displayData() { cout << ""; cout << "

Dyna Telecommunications Inc.

"; cout << "This is the Customer Details Update Form. Please enter the correct data. The format for Date of Birth is yyyy-mm-dd."; cout << "

Application Form

"; cout << "

" << errorMessage << "

"; cout << "
"; cout << "
"
         << "         Mobile Telephone Number: 
\n" << " Customer Name:
\n" << " Password:
\n" << " Date of Birth:  yyyy-mm-dd
\n" << " Address:
\n" << " City:
\n" << " Email:
\n" << "
"; cout << "
"; cout << "

" << errorMessage << "

"; cout << ""; errorMessage = " "; } void displayStart() { cout << ""; cout << "

Dyna Telecommunications Inc.

"; cout << "Thank you. Your details have been saved."; cout << "
"; cout << "

New Customer

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

Existing Customer

"; cout << "
"; cout << "
          Mobile Telephone Number: 
Password:
"; cout << "
"; cout << ""; } void insertRecord() { updateDB(cust); } }; int main() { WebProcess web; web.get(); web.urlDecode(); web.split(); if(web.validateData() == false) { web.displayData(); } else { web.insertRecord(); web.displayStart(); } return 0; }