Summary of database addition deletion modification and search operation implemented by CI framework of CodeIgniter
- 2021-10-11 17:55:28
- OfStack
This article describes the example of CI framework (CodeIgniter) to achieve the operation of adding, deleting, modifying and checking database. Share it for your reference, as follows:
cquery. php file under controllers
<?php
class CQuery extends Controller {
// Constructor
function CQuery() {
parent::Controller();
// $this->load->database();
}
function index() {
// Call model Among them train For outer folders MQuery For model Name queryList For renaming
$this->load->model('train/MQuery','queryList');
// Get the returned result set Here it is determined that the call model Which method in
$result = $this->queryList->queryList();
// Assign the result set to the res
$this->smarty->assign('res',$result);
// Jump to display page
$this->smarty->view('train/vquery.tpl');
}
// Enter the new page
function addPage() {
$this->smarty->view('train/addPage.tpl');
}
// Add
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');
// Gender
$sex = $this->input->post('sex');
// Date of Birth
$bornDay = $this->input->post('bornDay');
//e_mail
$eMail = $this->input->post('eMail');
// Cryptographic problem
$question = $this->input->post('question');
// Password answer
$answer = $this->input->post('answer');
// Call model
$this->load->model('train/MQuery','addRecord');
// Toward model In addRecord Value transfer
$result = $this->addRecord->addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
// Judge the returned result , If you return true, Call the index Method , Don't write $result == false Because the returned value may not be false It could also be ""
if ($result) {
$this->index();
} else {
echo "add failed.";
}
}
// Delete
function deletePage() {
// Obtain ID
$deleteID = $this->uri->segment(4);
// Call model
$this->load->model('train/MQuery','delRecord');
// Pass a value into the model Adj. delRecord Method
$result = $this->delRecord->delRecord($deleteID);
// Judge 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->changeRecord->changeRecord($changeID);
// Assign the result set to the 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');
// Gender
$sex = $this->input->post('sex');
// Date of Birth
$bornDay = $this->input->post('bornDay');
//e_mail
$eMail = $this->input->post('eMail');
// Cryptographic problem
$question = $this->input->post('question');
// Password answer
$answer = $this->input->post('answer');
// Call model
$this->load->model('train/MQuery','change');
// Toward model In change Value transfer
$result = $this->change->change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
// Judge the returned result , If you return true, Call the index Method , Don't write $result == false Because the returned value may not be false It could also be ""
if ($result) {
$this->index();
} else {
echo "change failed.";
}
}
}
mquery. php file in models
<?php
class MQuery extends Model {
// Constructor
function MQuery() {
parent::Model();
// Connect to a database
$this->load->database();
}
// Query list
function queryList() {
// Prevent select There is a garbled problem in the output data
//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 the database
$this->db->close();
// Returns the result set
return $result;
}
// Add
function addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
// Prevent select There is a garbled problem in the output data
//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 the database
$this->db->close();
// Return value
return $result;
}
// Delete
function delRecord($deleteID) {
// Prevent select There is a garbled problem in the output data
//mysql_query("SET NAMES GBK");
$sql = "DELETE FROM user_info_t WHERE ID = $deleteID";
$result = $this->db->query($sql);
$this->db->close();
return $result;
}
// Query before modification
function changeRecord($changeID) {
// Prevent select There is a garbled problem in the output data
//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 the database
$this->db->close();
// Returns the result set
return $result;
}
// Modify
function change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
// Prevent select There is a garbled problem in the output data
//mysql_query("SET NAMES GBK");
//SQL Statement
$sql = "update user_info_t set member_name = '$memberName',password = '$password', user_real_name = '$userRealName'," .
"sex = '$sex',born_day = '$bornDay',e_mail = '$eMail',question = '$question',answer = '$answer'" .
"where ID = $ID";
// Execute SQL
$result = $this->db->query($sql);
// Close the database
$this->db->close();
// Return value
return $result;
}
}
addPage. tpl file under views
<html>
<head>
</head>
<body><form action="{{site_url url='train/cquery/add'}}" method="post">
<table border='1'>
<tr>
<td> User name </td>
<td><input type="text" class="text" name="memberName" id="memberName"/></td>
</tr>
<tr>
<td> Password </td>
<td><input type="text" class="text" name="password" id="password"/></td>
</tr>
<tr>
<td> Real name </td>
<td><input type="text" class="text" name="userRealName" id="userRealName"/></td>
</tr>
<tr>
<td> Gender </td>
<td><input type="text" class="text" name="sex" id="sex"/></td>
</tr>
<tr>
<td> Date of Birth </td>
<td><input type="text" class="text" name="bornDay" id="bornDay"/></td>
</tr>
<tr>
<td>e_mail</td>
<td><input type="text" class="text" name="eMail" id="eMail"/></td>
</tr>
<tr>
<td> Cryptographic problem </td>
<td><input type="text" class="text" name="question" id="question"/></td>
</tr>
<tr>
<td> Password answer </td>
<td><input type="text" class="text" name="answer" id="answer"/></td>
</tr>
</table>
<table>
<tr>
<td><input type="submit" class="button" name="OK" value=" Submit " />
</td>
</tr>
</table></form>
</body>
</html>
File changePage. tpl
<html>
<head>
</head>
<body><form action="{{site_url url='train/cquery/change'}}" method="post">
<table border='1'><input type="hidden" name="id" value="{{$res->ID}}" />
<tr>
<td> User name </td>
<td><input type="text" class="text" name="memberName" id="memberName" value="{{$res->member_name}}" /></td>
</tr>
<tr>
<td> Password </td>
<td><input type="text" class="text" name="password" id="password" value="{{$res->password}}" /></td>
</tr>
<tr>
<td> Real name </td>
<td><input type="text" class="text" name="userRealName" id="userRealName" value="{{$res->user_real_name}}"/></td>
</tr>
<tr>
<td> Gender </td>
<td><input type="text" class="text" name="sex" id="sex" value="{{$res->sex}}"/></td>
</tr>
<tr>
<td> Date of Birth </td>
<td><input type="text" class="text" name="bornDay" id="bornDay" value="{{$res->born_day}}"/></td>
</tr>
<tr>
<td>e_mail</td>
<td><input type="text" class="text" name="eMail" id="eMail" value="{{$res->e_mail}}"/></td>
</tr>
<tr>
<td> Cryptographic problem </td>
<td><input type="text" class="text" name="question" id="question" value="{{$res->question}}"/></td>
</tr>
<tr>
<td> Password answer </td>
<td><input type="text" class="text" name="answer" id="answer" value="{{$res->answer}}"/></td>
</tr>
</table>
<table>
<tr>
<td><input type="submit" class="button" name="OK" value=" Submit " />
</td>
</tr>
</table></form>
</body>
</html>
vquery. tpl file
<html>
<head>
<title></title>
</head>
<body>
<table border='1'>
<tr>
<td> User name </td>
<td> Gender </td>
<td>e_mail</td>
<td> Operation </td>
</tr>
{{foreach from=$res item=row}}
<tr>
<input type="hidden" value={{$row->ID}}>
<td>{{$row->member_name}}</td>
<td>{{$row->sex}}</td>
<td>{{$row->e_mail}}</td>
<td><a href="{{site_url url='train/cquery/deletePage'}}/{{$row->ID}}" rel="external nofollow" > Delete </a><a href="{{site_url url='train/cquery/changePage'}}/{{$row->ID}}" rel="external nofollow" > Modify </a></td>
</tr>
{{/foreach}}
</table>
<a href="{{site_url url='train/cquery/addPage'}}" rel="external nofollow" rel="external nofollow" mce_href="{{site_url url='train/cquery/addPage'}}" rel="external nofollow" rel="external nofollow" >add</a>
</body>
</html>
For more readers interested in CodeIgniter related content, please check the topics on this site: "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "php Excellent Development Framework Summary", "ThinkPHP Introduction Tutorial", "ThinkPHP Common Methods Summary", "Zend FrameWork Framework Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php+mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"
I hope this article is helpful to the PHP programming based on CodeIgniter framework.