Using C for CGI Programming(3)
2010-09-18 12:18 shuisheng 阅读(480) 评论(0) 收藏 举报Finally, I need a way to handle the submit buttons. They're the mostcomplex input, because I need to launch a function based on theirvalues and select a default value, just in case. The cgic library hasa function, cgiFormSelectSingle(), that emulates this behavior exactly.It requires the list of possible values to be in an array of strings.It populates an integer variable with the index of the parameter inthe array or uses a default value if there are no matches.
See Resources for information on function pointers.If function pointers stillbaffle you, you can choose the function to run in a switchstatement. I prefer the array of function pointers because it is morecompact, but my older code still makes use of the switch statement.
MySQL from C is largely the same as PHP, if you're used to thatinterface. You have to use MySQL's string escape functions toescape problematic characters in your strings, such as quote charactersor the back slash character, but otherwise it is basicallythe same.The show_event() function requires me to fetch a single record fromthe primary key. All of the error checking bulks up the code, butit's really three basic statements.A call to mysql_query() executes the MySQL statement and generates aresult set. A call to mysql_store_result() retrieves the result setfrom the server. Finally, a call to mysql_fetch_row() pulls a singleMYSQL_ROW variable from the result set.
The MYSQL_ROW variable can be treated like an array of strings(char**). If any of the data is numeric and you want to treat it asnumeric data, you need to convert it. For instance, in myapplication it is desirable to have the date as three separate numericcomponents. Because this data is structured as YYYY-MM-DD, I usesscanf() to get the components (Listing 6).
Writing data to the database is more interesting because of the needto escape the data. Listing 7 shows how it is done.
escapedname holds the same string as name, with MySQL specialcharacters escaped so I can insert them into an SQL statementwithout worry. It is essential that you escape all strings read fromuser input; otherwise, a devious person could take advantage of yourlapse and do unpleasant things to your database.
 
                    
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号