A summary of the implementation of the CI Framework (codeigniter) for database additions and deletions

Source: Internet
Author: User
This article mainly introduces the implementation of the CI Framework (codeigniter) database additions and deletions, combined with examples to summarize and analyze the CI framework for MySQL database additions and deletions of the model, controller and view related definitions and use of skills, the need for friends can refer to the next

This paper describes the database additions and deletions of CI framework (CodeIgniter) implementation. Share to everyone for your reference, as follows:

cquery.php Files under Controllers

<?phpclass Cquery extends Controller {//constructor function Cquery () {Parent::controller ();//$this->load->dat  Abase (); } function Index () {//Call model where train is the outer folder mquery the model name querylist rename $this->load->model (' Train/mquery ',    ' Querylist ');    Get the result set returned here determine which method in the call model $result = $this->querylist->querylist ();    Assign the result set to res $this->smarty->assign (' res ', $result);  Jump to display page $this->smarty->view (' Train/vquery.tpl ');  }//Enter new page function AddPage () {$this->smarty->view (' Train/addpage.tpl ');    }//new function Add () {//Get foreground data//user name $memberName = $this->input->post (' membername ');    Password $password = $this->input->post (' password ');    real name $userRealName = $this->input->post (' userrealname ');    Sex $sex = $this->input->post (' sex ');    Date of birth $bornDay = $this->input->post (' Bornday ');    E_Mail $eMail = $this->input->post (' EMail '); Password Problem $question = $this->input->post (' question ');    Password answer $answer = $this->input->post (' answer ');    Call Model $this->load->model (' train/mquery ', ' AddRecord '); Pass a value $result = $this->addrecord->addrecord ($memberName, $password, $userRealName, $sex, $ addrecord in model    Bornday, $eMail, $question, $answer);    Judging the returned result, if true, call the index method on this page, do not write $result = = False because the returned value may not be false or "" if ($result) {$this->index ();    } else {echo "Add failed.";    }}//delete function Deletepage () {//get id $deleteID = $this->uri->segment (4);    Call Model $this->load->model (' train/mquery ', ' Delrecord ');    The value is passed into the model's Delrecord method $result = $this->delrecord->delrecord ($deleteID);    Determine the return value if ($result) {$this->index ();    } else {echo "Delect failed.";    }}//Modify first query function Changepage () {$changeID = $this->uri->segment (4);    $this->load->model (' train/mquery ', ' Changerecord '); $result = $this-&GT;CHANGERECORD-&GT;changerecord ($changeID);    Assign the result set to res $this->smarty->assign (' res ', $result);  Jump to display page $this->smarty->view (' Train/changepage.tpl ');    }//Modify function Change () {//Get foreground data//id $ID = $this->input->post (' ID ');    User name $memberName = $this->input->post (' membername ');    Password $password = $this->input->post (' password ');    real name $userRealName = $this->input->post (' userrealname ');    Sex $sex = $this->input->post (' sex ');    Date of birth $bornDay = $this->input->post (' Bornday ');    E_Mail $eMail = $this->input->post (' EMail ');    Password problem $question = $this->input->post (' question ');    Password answer $answer = $this->input->post (' answer ');    Call Model $this->load->model (' train/mquery ', ' change '); To change the value in model $result = $this->change->change ($ID, $memberName, $password, $userRealName, $sex, $bornDay, $    EMail, $question, $answer); Determine the returned result, if true, then call the index method on this page, do not write $result = =False because the returned value may not be false or "" if ($result) {$this->index ();    } else {echo "change failed."; }  }}

mquery.php files in Models

<?phpclass Mquery extends Model {//constructor function Mquery () {Parent::model ();  Connect database $this->load->database ();    }//Query list function querylist () {//Prevent the data in select out of garbled problem//mysql_query ("SET NAMES GBK");    SQL statement $sql = "Select Id,member_name,sex,e_mail from user_info_t";    Execute SQL $rs = $this->db->query ($sql);    Put the query results into the result set $result = $rs->result ();    Close Database $this->db->close ();  Returns the result set to the return $result; }//New function AddRecord ($memberName, $password, $userRealName, $sex, $bornDay, $eMail, $question, $answer) {//    Prevent the data of select out of garbled problem//mysql_query ("SET NAMES GBK");        SQL statement $sql = "INSERT into user_info_t (member_name,password,user_real_name,sex,born_day,e_mail,question,answer)".    "VALUES (' $memberName ', ' $password ', ' $userRealName ', ' $sex ', ' $bornDay ', ' $eMail ', ' $question ', ' $answer ')";    Execute SQL $result = $this->db->query ($sql);    Close Database $this->db->close ();  Return a value of return $result; }//Delete function Delrecord ($deleteID) {//prevents the data in the select from being garbled//mysql_query ("SET NAMES GBK");    $sql = "DELETE from user_info_t WHERE ID = $deleteID";    $result = $this->db->query ($sql);    $this->db->close ();  return $result;    }//Modify the Query function Changerecord ($changeID) {//Prevent the data of select out of garbled problem//mysql_query ("SET NAMES GBK"); $sql = "Select Id,member_name,password,user_real_name,sex,born_day,e_mail,question,answer from user_info_t WHERE ID = $    Changeid ";    Execute SQL $rs = $this->db->query ($sql);    $result = $rs->row ();//$result = $rs [0]//Close database $this->db->close ();  Returns the result set to the return $result; }//Modify function Change ($ID, $memberName, $password, $userRealName, $sex, $bornDay, $eMail, $question, $answer) {//    Prevent the data of select out of garbled problem//mysql_query ("SET NAMES GBK"); SQL statement $sql = "Update user_info_t set member_name = ' $memberName ', password = ' $password ', user_real_name = ' $userRealN        Ame ', ". "Sex = ' $sex ', born_day = ' $bornDay ', E_Mail = ' $eMail ', question = ' $question ', answer = ' $answer ' ".    "Where ID = $ID";    Execute SQL $result = $this->db->query ($sql);    Close Database $this->db->close ();  Return a value of return $result; }}

Views of the Addpage.tpl file

CHANGEPAGE.TPL file

VQUERY.TPL file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.