Thread: How to insert/update multiple records in web envirnoment

How to insert/update multiple records in web envirnoment

From
"Jeff"
Date:
Hi,

When there's only one recorde to Insert/updat, the server cgi works fine.

When try to insert/update multiple records the cgi keeps running and never
ends.

Here's the code snippet:

      while(ip)
      {
        strcpy(buffer, ip->Item);
        ip = ip->next;
        while(strstr(buffer, "``")) replace_str(buffer, "``", "` `");
        sscanf(buffer,
"%[^`]`%[^`]`%[^`]`%[^`]`%[^`]`%[^`]`%[^`]`%[^`]`%[^`]`%[^`]`%[^`]`%[^`]`%[^
`]`%[^`]`%[^`]`s",
                   op.key, op.last, op.first, op.jobcode, op.address,
op.city, op.state, op.zip,
                   op.phone, op.id, op.password, op.notes, op.status,
op.permissions, op.wages);

        sprintf(query_string, "SELECT * FROM employees WHERE key = '%s'",
op.key);
        PQclear(res);
        res = PQexec(conn, query_string);

        if (PQresultStatus(res) != PGRES_TUPLES_OK)         /* did the query
fail? */
        {
          postMessage("SELECT query failed", 0);
          PQclear(res);
          PQfinish(conn);
          exit(1);
        }

        if(PQntuples(res) == 0)
        {
          sprintf(query_string, "INSERT INTO employees "
                                "(startdate, last, first, jobcode, address,
city, state, zip, "
                                "phone, id, password, notes, status,
permissions, wages) "
                                "VALUES ('%s', '%s', '%s', '%s', '%s',
'%s',"
                                        "'%s', '%s', '%s', '%s', '%s',"
                                        "'%s', '%s', '%s', '%s')",
                   today, op.last, op.first, op.jobcode, op.address,
op.city, op.state, op.zip,
                   op.phone, op.id, op.password, op.notes, op.status,
op.permissions, op.wages);
          PQclear(res);
          res = PQexec(conn, query_string);
        }
       else  {
          sprintf(query_string, "UPDATE employees SET lastmodified = '%s',"
                                                 "last = '%s',"
                                                 "first = '%s',"
                                                 "jobcode = '%s',"
                                                 "address = '%s',"
                                                 "city = '%s',"
                                                 "state = '%s',"
                                                 "zip = '%s',"
                                                 "phone = '%s',"
                                                 "id = '%s',"
                                                 "password = '%s',"
                                                 "notes = '%s',"
                                                 "status = '%s',"
                                                 "permissions = '%s',"
                                                 "wages = '%s'"
                                                 "WHERE key = '%s'",
                   today, op.last, op.first, op.jobcode, op.address,
op.city, op.state, op.zip,
                   op.phone, op.id, op.password, op.notes, op.status,
op.permissions, op.wages, op.key);
          PQclear(res);
          res = PQexec(conn, query_string);

         ip = ip->next;
       }
     }
    PQclear(res);
    PQfinish(conn);
}