php实战第二天_PHP教程原创2016-07-14 10:10:4702626
1.我的留言板上写上了 add方法
[php] function add(){
//添加后返回 影响条数,如果大于0就说明添加成功
if($this->db->data($_POST)->add()>0){
echo "添加成功";
// echo "";//防止刷新后的表单的重复提交
Header("HTTP/1.1 303 See Other");
Header("Location: "); //转向到根目录
exit;
}else {
die($this->db->error());//添加失败输出错误信息
}
}
function add(){
//添加后返回 影响条数,如果大于0就说明添加成功
if($this->db->data($_POST)->add()>0){
echo "添加成功";
// echo "";//防止刷新后的表单的重复提交
Header("HTTP/1.1 303 See Other");
Header("Location: "); //转向到根目录
exit;
}else {
die($this->db->error());//添加失败输出错误信息
}
}对应提交表单
[html]
用户名
留言内容
aaaaa
电子邮箱
email:
用户名
留言内容
aaaaa
电子邮箱
email:
2.修改了MYSQL操作类 使 data 自动处理传递过来的数据,对比是否存在字段,不存在不添加.
[php]
// echo 'add_sql'.$add_sql;
return $this->execute($add_sql);
}
/**
+----------------------------------------------------------
* 删除
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function delete(){
$del_sql = 'delete from `'.$this->query_list['table'].'` where '.$this->query_list['where'];
if(isset($this->query_list['order']))
$del_sql .= 'order by '.$this->query_list['order'];
if(isset($this->query_list['limit']))
$del_sql .= ' '.$this->query_list['limit'];
return $this->execute($del_sql);
}
/**
+----------------------------------------------------------
* 更新
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function update(){
$update_sql = 'update `'.$this->query_list['table'].'` set ';
$data = $this->query_list['data'];
foreach($data as $k=>$v){
if(is_numeric($v))
$update_sql .= '`'.$k.'` ='.$v.',';
else
$update_sql .= '`'.$k.'` =\''.$v.'\',';
}
$update_sql = rtrim($update_sql,',');
if(isset($this->query_list['where']))
$update_sql .= ' where '.$this->query_list['where'];
if(isset($this->query_list['order']))
$update_sql .= ' order by '.$this->query_list['order'];
if(isset($this->query_list['limit']))
$update_sql .= ' '.$this->query_list['limit'];
return $this->execute($update_sql);
}
/**
+----------------------------------------------------------
* 执行查询 返回数据集
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $sql sql指令
*/
public function query($sql) {
if ( !$this->conn ) return false;
$this->queryStr = $sql;
//释放前次的查询结果
if ( $this->queryID ) { $this->free(); }
$this->query_start_time = microtime(true);
$this->queryID = mysql_query($sql, $this->conn);
$this->query_count++;
if ( false === $this->queryID ) {
$this->error();
return false;
} else {
$this->numRows = mysql_num_rows($this->queryID);
return $this->getAll();
}
}
/**
+----------------------------------------------------------
* 执行语句
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $sql sql指令
+----------------------------------------------------------
*/
public function execute($sql) {
if ( !$this->conn ) return false;
$this->queryStr = $sql;
//释放前次的查询结果
if ( $this->queryID ) { $this->free(); }
$this->query_start_time = microtime(true);
$result = mysql_query($sql, $this->conn) ;
$this->query_count++;
if ( false === $result) {
$this->error();
return false;
} else {
$this->numRows = mysql_affected_rows($this->conn);
return $this->numRows;
}
}
/**
+----------------------------------------------------------
* 获得所有的查询数据
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
* @return array
*/
private function getAll() {
//返回数据集
$result = array();
if($this->numRows &g