⑴ php CI框架修改數據的方法
CI框架下的PHP增刪改查總結:
controllers下的 cquery.php文件
[php] view plain
<?php
class CQuery extends Controller {
//構造函數
function CQuery() {
parent::Controller();
// $this->load->database();
}
function index() {
//調用model 其中train為外層文件夾 MQuery為model名稱 queryList為重命名
$this->load->model('train/MQuery','queryList');
//獲得返回的結果集 這里確定調用model中的哪個方法
$result = $this->queryList->queryList();
//將結果集賦給res
$this->smarty->assign('res',$result);
//跳轉到顯示頁面
$this->smarty->view('train/vquery.tpl');
}
//進入新增頁面
function addPage() {
$this->smarty->view('train/addPage.tpl');
}
//新增
function add() {
//獲得前台數據
//用戶名
$memberName = $this->input->post('memberName');
//密碼
$password = $this->input->post('password');
//真實姓名
$userRealName = $this->input->post('userRealName');
//性別
$sex = $this->input->post('sex');
//出生日期
$bornDay = $this->input->post('bornDay');
//e_mail
$eMail = $this->input->post('eMail');
//密碼問題
$question = $this->input->post('question');
//密碼答案
$answer = $this->input->post('answer');
//調用model
$this->load->model('train/MQuery','addRecord');
//向model中的addRecord傳值
$result = $this->addRecord->addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
//判斷返回的結果,如果返回true,則調用本頁的index方法,不要寫 $result == false 因為返回的值未必是false 也有可能是""
if ($result) {
$this->index();
} else {
echo "add failed.";
}
}
//刪除
function deletePage() {
//獲得ID
$deleteID = $this->uri->segment(4);
//調用model
$this->load->model('train/MQuery','delRecord');
//將值傳入到model的delRecord方法中
$result = $this->delRecord->delRecord($deleteID);
//判斷返回值
if ($result) {
$this->index();
} else {
echo "delect failed.";
}
}
//修改先查詢
function changePage() {
$changeID = $this->uri->segment(4);
$this->load->model('train/MQuery','changeRecord');
$result = $this->changeRecord->changeRecord($changeID);
//將結果集賦給res
$this->smarty->assign('res',$result);
//跳轉到顯示頁面
$this->smarty->view('train/changePage.tpl');
}
//修改
function change() {
//獲得前台數據
//ID
$ID = $this->input->post('id');
//用戶名
$memberName = $this->input->post('memberName');
//密碼
$password = $this->input->post('password');
//真實姓名
$userRealName = $this->input->post('userRealName');
//性別
$sex = $this->input->post('sex');
//出生日期
$bornDay = $this->input->post('bornDay');
//e_mail
$eMail = $this->input->post('eMail');
//密碼問題
$question = $this->input->post('question');
//密碼答案
$answer = $this->input->post('answer');
//調用model
$this->load->model('train/MQuery','change');
//向model中的change傳值
$result = $this->change->change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
//判斷返回的結果,如果返回true,則調用本頁的index方法,不要寫 $result == false 因為返回的值未必是false 也有可能是""
if ($result) {
$this->index();
} else {
echo "change failed.";
}
}
}
models中的 mquery.php 文件
[php] view plain
<?php
class MQuery extends Model {
//構造函數
function MQuery() {
parent::Model();
//連接資料庫
$this->load->database();
}
//查詢列表
function queryList() {
//防止select出的數據存在亂碼問題
//mysql_query("SET NAMES GBK");
//SQL語句
$sql = "SELECT ID,member_name,sex,e_mail FROM user_info_t";
//執行SQL
$rs = $this->db->query($sql);
//將查詢結果放入到結果集中
$result = $rs->result();
//關閉資料庫
$this->db->close();
//將結果集返回
return $result;
}
//新增
function addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
//防止select出的數據存在亂碼問題
//mysql_query("SET NAMES GBK");
//SQL語句
$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')";
//執行SQL
$result = $this->db->query($sql);
//關閉資料庫
$this->db->close();
//返回值
return $result;
}
//刪除
function delRecord($deleteID) {
//防止select出的數據存在亂碼問題
//mysql_query("SET NAMES GBK");
$sql = "DELETE FROM user_info_t WHERE ID = $deleteID";
$result = $this->db->query($sql);
$this->db->close();
return $result;
}
//修改前查詢
function changeRecord($changeID) {
//防止select出的數據存在亂碼問題
//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";
//執行SQL
$rs = $this->db->query($sql);
$result = $rs->row();//$result = $rs[0]
//關閉資料庫
$this->db->close();
//將結果集返回
return $result;
}
//修改
function change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
//防止select出的數據存在亂碼問題
//mysql_query("SET NAMES GBK");
//SQL語句
$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";
//執行SQL
$result = $this->db->query($sql);
//關閉資料庫
$this->db->close();
//返回值
return $result;
}
}
views 下的 addPage.tpl文件
[php] view plain
<html>
<head>
</head>
<body><form action="{{site_url url='train/cquery/add'}}" method="post">
<table border='1'>
<tr>
<td>用戶名</td>
<td><input type="text" class="text" name="memberName" id="memberName"/></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="text" class="text" name="password" id="password"/></td>
</tr>
<tr>
<td>真實姓名</td>
<td><input type="text" class="text" name="userRealName" id="userRealName"/></td>
</tr>
<tr>
<td>性別</td>
<td><input type="text" class="text" name="sex" id="sex"/></td>
</tr>
<tr>
<td>出生日期</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>密碼問題</td>
<td><input type="text" class="text" name="question" id="question"/></td>
</tr>
<tr>
<td>密碼答案</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="提交" />
</td>
</tr>
</table></form>
</body>
</html>
⑵ php迅速做簡單的動態網站( 只需要 能夠在後台更新文字和圖片)
這們朋友,你要實現更新文字和圖片功能,需要有知道以下知識點:
1)資料庫的增刪改查操作;
2)使用PHP對資料庫進行操作(涉及到表單提交);
3)用PHP使結果循環遍歷輸出;
4)分頁功能實現;
5)文件上傳(主要針對圖片更新)
以上幾點你只要會了,你所說的功能就可以簡單實現。當然,還有個笨辦法,就是你做靜態網頁,要添加的內容可以用DW的可視化編輯添加好內容然後上傳到伺服器後做好相關鏈接,這樣就不涉及到資料庫操作,也不用PHP,只是每次更新的內容只存在靜態網頁中,無法保存到資料庫里。兩者你可以任選其一了。
(Mekbo【麥客博】)
⑶ 在php中如何對已經上傳的圖片進行增刪查改,並得到當前圖片儲存的位置
php 上傳圖片,在資料庫里存放的都是圖片地址,對圖片操作實際上就是資料庫存放圖片的地址進行增刪改查的 操作;刪除圖片的話,可以用php 的 unlink 函數,先把數據的圖片地址查詢出來,用php查一下這個圖片是否存在,存在的話,刪除,改 就是 上傳一個同名文件,把源文件覆蓋
⑷ 求php增刪改查代碼。
class sqlHelper{
public $conn;
public $dbname="資料庫名稱";
public $username="資料庫用戶名";
public $password="資料庫密碼";
public $host="localhost";
//連接資料庫
public function __construct(){
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if(!$this->conn){
die("連接失敗".mysql_error());
}
mysql_select_db($this->dbname,$this->conn);
}
//執行查詢語句
public function execute_dql($sql){
$res=mysql_query($sql,$this->conn);
return $res;
}
//執行增填改語句
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn);
if(!$b){
return 3;
}else{
if(mysql_affected_rows($this->conn)){
return 1;//表示OK
}else{
return 2;//表示沒有行收到影響
}
}
}
}
⑸ 有什麼增刪改查代碼生成器可以推薦
ListCode.cn是一個在線的增刪改查代碼生成器,無需下載安裝,只需提供資料庫腳本即可生成所需的代碼。
該工具對資料庫外鍵關系的支持尤為出色,可以自動生成相關的關聯代碼,極大地方便了開發者的工作。
它提供包括Java、Python、PHP和Node.js在內的多種主流編程語言及框架的CRUD代碼生成服務,涵蓋了後台代碼、前端界面以及相關的JavaScript代碼。
與傳統的增刪改查代碼生成器相比,ListCode.cn在功能豐富性和支持的語言、技術框架種類上都有了顯著提升。
使用它可以顯著提高開發效率,減少人工干預修改代碼的工作量,從而讓開發者能夠更加專注於業務邏輯的實現。
該工具具有以下功能特點:
1. 多語言支持:能夠生成Java、Python、PHP和Node.js等主流編程語言的代碼。
2. 框架兼容性:兼容多種主流開發框架,如Spring、Django、Laravel和Express等。
3. 自動化生成:能夠根據資料庫結構自動生成增刪改查功能的代碼。
4. 高度可配置:用戶可以根據自己的需求調整生成代碼的細節,如表單樣式、資料庫連接配置等。
5. 前後端分離:不僅能夠生成後台處理代碼,還能生成前端界面代碼,方便前後端分離開發。
6. 外鍵支持:能夠識別資料庫中的外鍵關系,並自動生成相關的關聯代碼。
7. 社區支持:擁有活躍的開發者社區,用戶可以在這里尋求幫助、分享經驗。
8. 免費使用:部分功能免費開放,適合小型項目和個人開發者使用。