php第二天,php实战第二天_PHP教程

php实战第二天_PHP教程原创2016-07-14 10:10:4702626

0d41b01f31a2f54a2beb2ff307129c6e.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值