php防數(shù)據(jù)修改 PHP修改
PHP CI框架修改數(shù)據(jù)的方法
CI框架下的PHP增刪改查總結(jié):
成都創(chuàng)新互聯(lián)專(zhuān)注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、天全網(wǎng)絡(luò)推廣、重慶小程序開(kāi)發(fā)公司、天全網(wǎng)絡(luò)營(yíng)銷(xiāo)、天全企業(yè)策劃、天全品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供天全建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
controllers下的 cquery.php文件
[php] view plain copy
?php
class CQuery extends Controller {
//構(gòu)造函數(shù)
function CQuery() {
parent::Controller();
// $this-load-database();
}
function index() {
//調(diào)用model 其中train為外層文件夾 MQuery為model名稱(chēng) queryList為重命名
$this-load-model('train/MQuery','queryList');
//獲得返回的結(jié)果集 這里確定調(diào)用model中的哪個(gè)方法
$result = $this-queryList-queryList();
//將結(jié)果集賦給res
$this-smarty-assign('res',$result);
//跳轉(zhuǎn)到顯示頁(yè)面
$this-smarty-view('train/vquery.tpl');
}
//進(jìn)入新增頁(yè)面
function addPage() {
$this-smarty-view('train/addPage.tpl');
}
//新增
function add() {
//獲得前臺(tái)數(shù)據(jù)
//用戶名
$memberName = $this-input-post('memberName');
//密碼
$password = $this-input-post('password');
//真實(shí)姓名
$userRealName = $this-input-post('userRealName');
//性別
$sex = $this-input-post('sex');
//出生日期
$bornDay = $this-input-post('bornDay');
//e_mail
$eMail = $this-input-post('eMail');
//密碼問(wèn)題
$question = $this-input-post('question');
//密碼答案
$answer = $this-input-post('answer');
//調(diào)用model
$this-load-model('train/MQuery','addRecord');
//向model中的addRecord傳值
$result = $this-addRecord-addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
//判斷返回的結(jié)果,如果返回true,則調(diào)用本頁(yè)的index方法,不要寫(xiě) $result == false 因?yàn)榉祷氐闹滴幢厥莊alse 也有可能是""
if ($result) {
$this-index();
} else {
echo "add failed.";
}
}
//刪除
function deletePage() {
//獲得ID
$deleteID = $this-uri-segment(4);
//調(diào)用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);
//將結(jié)果集賦給res
$this-smarty-assign('res',$result);
//跳轉(zhuǎn)到顯示頁(yè)面
$this-smarty-view('train/changePage.tpl');
}
//修改
function change() {
//獲得前臺(tái)數(shù)據(jù)
//ID
$ID = $this-input-post('id');
//用戶名
$memberName = $this-input-post('memberName');
//密碼
$password = $this-input-post('password');
//真實(shí)姓名
$userRealName = $this-input-post('userRealName');
//性別
$sex = $this-input-post('sex');
//出生日期
$bornDay = $this-input-post('bornDay');
//e_mail
$eMail = $this-input-post('eMail');
//密碼問(wèn)題
$question = $this-input-post('question');
//密碼答案
$answer = $this-input-post('answer');
//調(diào)用model
$this-load-model('train/MQuery','change');
//向model中的change傳值
$result = $this-change-change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
//判斷返回的結(jié)果,如果返回true,則調(diào)用本頁(yè)的index方法,不要寫(xiě) $result == false 因?yàn)榉祷氐闹滴幢厥莊alse 也有可能是""
if ($result) {
$this-index();
} else {
echo "change failed.";
}
}
}
models中的 mquery.php 文件
[php] view plain copy
?php
class MQuery extends Model {
//構(gòu)造函數(shù)
function MQuery() {
parent::Model();
//連接數(shù)據(jù)庫(kù)
$this-load-database();
}
//查詢列表
function queryList() {
//防止select出的數(shù)據(jù)存在亂碼問(wèn)題
//mysql_query("SET NAMES GBK");
//SQL語(yǔ)句
$sql = "SELECT ID,member_name,sex,e_mail FROM user_info_t";
//執(zhí)行SQL
$rs = $this-db-query($sql);
//將查詢結(jié)果放入到結(jié)果集中
$result = $rs-result();
//關(guān)閉數(shù)據(jù)庫(kù)
$this-db-close();
//將結(jié)果集返回
return $result;
}
//新增
function addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
//防止select出的數(shù)據(jù)存在亂碼問(wèn)題
//mysql_query("SET NAMES GBK");
//SQL語(yǔ)句
$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')";
//執(zhí)行SQL
$result = $this-db-query($sql);
//關(guān)閉數(shù)據(jù)庫(kù)
$this-db-close();
//返回值
return $result;
}
//刪除
function delRecord($deleteID) {
//防止select出的數(shù)據(jù)存在亂碼問(wèn)題
//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出的數(shù)據(jù)存在亂碼問(wèn)題
//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";
//執(zhí)行SQL
$rs = $this-db-query($sql);
$result = $rs-row();//$result = $rs[0]
//關(guān)閉數(shù)據(jù)庫(kù)
$this-db-close();
//將結(jié)果集返回
return $result;
}
//修改
function change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
//防止select出的數(shù)據(jù)存在亂碼問(wèn)題
//mysql_query("SET NAMES GBK");
//SQL語(yǔ)句
$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";
//執(zhí)行SQL
$result = $this-db-query($sql);
//關(guān)閉數(shù)據(jù)庫(kù)
$this-db-close();
//返回值
return $result;
}
}
views 下的 addPage.tpl文件
[php] view plain copy
html
head
/head
bodyform action="{{site_url url='train/cquery/add'}}" method="post"
table border='1'
tr
td用戶名/td
tdinput type="text" class="text" name="memberName" id="memberName"http://td
/tr
tr
td密碼/td
tdinput type="text" class="text" name="password" id="password"http://td
/tr
tr
td真實(shí)姓名/td
tdinput type="text" class="text" name="userRealName" id="userRealName"http://td
/tr
tr
td性別/td
tdinput type="text" class="text" name="sex" id="sex"http://td
/tr
tr
td出生日期/td
tdinput type="text" class="text" name="bornDay" id="bornDay"http://td
/tr
tr
tde_mail/td
tdinput type="text" class="text" name="eMail" id="eMail"http://td
/tr
tr
td密碼問(wèn)題/td
tdinput type="text" class="text" name="question" id="question"http://td
/tr
tr
td密碼答案/td
tdinput type="text" class="text" name="answer" id="answer"http://td
/tr
/table
table
tr
tdinput type="submit" class="button" name="OK" value="提交" /
/td
/tr
/table/form
/body
/html
php里如果二個(gè)人同時(shí)操一個(gè)數(shù)據(jù)庫(kù)里表的字段,怎么避免
首先,你要知道,訪問(wèn)網(wǎng)站肯定存在先后,兩個(gè)人同時(shí)訪問(wèn)網(wǎng)站,哪怕只有1ms的時(shí)間差也會(huì)被識(shí)別出先后的順序。知道這個(gè)之后,對(duì)于避免數(shù)據(jù)庫(kù)被多人修改就很簡(jiǎn)單了,有多種實(shí)現(xiàn)方法,一種是借鑒信號(hào)量的應(yīng)用方法,在程序里來(lái)控制,誰(shuí)搶到誰(shuí)就擁有信號(hào)量,就可以操作數(shù)據(jù)庫(kù);另一種是使用數(shù)據(jù)庫(kù)ACID特性以及l(fā)ock功能(各種現(xiàn)代數(shù)據(jù)庫(kù)都支持事務(wù)處理模式并擁有l(wèi)ock功能,具體查你用的數(shù)據(jù)庫(kù)的文檔教程),先訪問(wèn)的要在程序里對(duì)數(shù)據(jù)庫(kù)加lock,防止其他人修改。
如何用php修改數(shù)據(jù)庫(kù)中的數(shù)據(jù)
舉例如下:
創(chuàng)建userinfo_update.php頁(yè)面用于查詢用戶信息,先顯示信息,在修改:
先通過(guò)GET獲取用戶編號(hào)查詢用戶信息:
$sql = "select * from user_info where user_id='".$_GET['userId']."'";
$result = mysql_query($sql,$con);
if($row = mysql_fetch_array($result)){
}
頁(yè)面效果:
創(chuàng)建update.php文件,用于修改用戶信息:
使用到了mysql_affected_rows() 函數(shù)返回前一次 MySQL 操作所影響的記錄行數(shù)。
//通過(guò)post獲取頁(yè)面提交數(shù)據(jù)信息
$userId = $_POST[userId];
$userName = $_POST[userName];
$userAge = $_POST[userAge];
$sql = "update user_info set user_name='".$userName."',user_age=".$userAge." where user_id='".$userId."'";
mysql_query($sql,$conn);//執(zhí)行SQL
$mark? = mysql_affected_rows();//返回影響行數(shù)
$url = "userinf_select.php";
運(yùn)行結(jié)果
創(chuàng)建delete.php文件,完成刪除用戶信息功能:
$userId = $_GET['userId'];
include 'connection.php';
$sql = "delete from user_info where user_id='".$userId."'";
mysql_query($sql,$con);
$mark? = mysql_affected_rows();//返回影響行數(shù)
if($mark0){
echo "刪除成功";
}else{
echo? "刪除失敗";
}
mysql_close($con);
運(yùn)行結(jié)果:
PHP數(shù)據(jù)修改
這個(gè)說(shuō)起來(lái)長(zhǎng)篇,你所問(wèn)的$updateSQL = $db-GetUpdateSQL其實(shí)并不是屬于php自己的東西,而是用戶自定義的類(lèi),至于類(lèi)是什么去看看基礎(chǔ)的php語(yǔ)言基礎(chǔ).
所以你要知道GetUpdateSQL返回的究竟是什么東西,他是怎么工作的,就要找到類(lèi)的本身代碼所在文件,去看看他里面究竟是什么東西.
而php修改數(shù)據(jù)庫(kù)里的東西其實(shí)是沒(méi)有專(zhuān)用語(yǔ)句的.如果硬要問(wèn)怎么實(shí)現(xiàn)的話,就是那個(gè)$db-Execute($updateSQL);
所以建議你把$updateSQL print(或echo)出屏幕看看里面是什么就明白了.
其實(shí)是個(gè)SQL的操作語(yǔ)句,指示SQL如何存儲(chǔ)數(shù)據(jù),而$db-Execute只讓php把這個(gè)命令傳給SQL
分享題目:php防數(shù)據(jù)修改 PHP修改
本文來(lái)源:http://www.xueling.net.cn/article/hippoi.html