Since I have been studying the xoops module for the past two days, I found this good module development quick start.
After reading it, I came to the development module with great interest, but encountered some problems during the development process.
I think it's too fast to learn. Therefore, the translation is here.
====================
Author: surance Yin
Email: Suranceyin@yahoo.com.cn
Home: http://www.fltek.com.cn
========================
Use Forms and databases
Now we areNdex. phpCreate a form
<? PHP // Tutorial // Created by kaotik Require ('HTTP: // www.cnblogs.com/mainfile.php '); Require (xoops_root_path. '/header. php '); ?> <Form name = "tutorial_form" method = "Post" Action = "index. php"> <Table width = "400" border = "0"> <Tr> <TD align = "right"> name </TD> <TD> <input type = "text" name = "name"> </TD> </Tr> <tr> <TD align = "right"> address </TD> <TD> <input type = "text" name =" Address "> </TD> </Tr> <tr> <TD align = "right"> telephone </TD> <TD> <input type = "text" name = "tel"> </TD> </Tr> <tr> <TD align = "right"> email </TD> <TD> <input type = "text" name = "email"> </TD> </Tr> <tr> <TD> & nbsp; </TD> <TD> <input type = "Submit" name = "Submit" value = "Submit"> </TD> </Tr> </Table> </Form> <? PHP Require (xoops_root_path. '/footer. php '); ?> |
click tutorial , you can see a file containing name, address, telephone and email , submit button form . Click " submit " will return to index. php however, no database operations are performed. Add the following Code :
// tutorial // created by kaotik require ('HTTP: // www.cnblogs.com/mainfile.php'); require (xoops_root_path. '/header. PHP '); If (isset ($ _ post ['submit ']) { echo 'My name is :'. $ _ post ['name']; } ?> |
Now I only show some of the above Code, which looks a little less..
Now, if you clickSubmitYou will see"My name is :"Your name is behind.
Here we have explainedPHP.
<? PHP // Tutorial // Created by kaotik Require ('HTTP: // www.cnblogs.com/mainfile.php '); Require (xoops_root_path. '/header. php '); If (isset ($ _ post ['submit ']) { If (empty ($ _ post ['name']) { Echo 'Please fill in a Name '; } Else { Echo 'My name is: '. $ _ post ['name']; } } ?> |
If the user name is not specified, an error is returned.
Here we have explainedPHP.
Store data to the database
Let's continue. Now we need to insert the data into the database.
<? PHP // Tutorial // Created by kaotik Require ('HTTP: // www.cnblogs.com/mainfile.php '); Require (xoops_root_path. '/header. php '); If (isset ($ _ post ['submit ']) { If (empty ($ _ post ['name']) { Echo 'Please fill in a Name '; } Else { $ Name = $ _ post ['name']; $ Address = $ _ post ['address']; $ Tel = $ _ post ['tel']; $ Email = $ _ post ['email ']; $ Query = "insert ". $ xoopsdb-> prefix ("tutorial_myform "). "(name, address, telephone, email) values ('$ name',' $ address', '$ Tel', '$ email ')"; $ Res = $ xoopsdb-> query ($ query ); If (! $ Res ){ Echo "error: $ query "; } Else { Echo "data was correctly inserted into DB! "; } } } ?> |
Before the new Code4Returns the form data to the variable. In this way, you can easily understand the content of the Code.The following $ QueryUsed in $ Xoopsdb:
$ Res = $ xoopsdb-> query ($ query ); |
The following line:
is used to check whether an exception occurs during database insertion. If yes, print " data was correctly inserted into DB! ". I will explain the following in detail .