创建商品表

#商品表

CREATE TABLE IF NOT EXISTS `goods` (

 `goods_id` int(10) unsigned NOT NULL AUTO_INCREMENT,

 `goods_sn` char(15) NOT NULL DEFAULT '',

 `cat_id` smallint(6) NOT NULL DEFAULT '0',

 `brand_id` smallint(6) NOT NULL DEFAULT '0',

 `goods_name` varchar(30) NOT NULL DEFAULT '',

 `shop_price` decimal(9,2) NOT NULL DEFAULT '0.00',

 `market_price` decimal(9,2) NOT NULL DEFAULT '0.00',

 `goods_number` smallint(6) NOT NULL DEFAULT '1',

 `click_count` mediumint(9) NOT NULL DEFAULT '0',

 `goods_weight` decimal(6,3) NOT NULL DEFAULT '0.000',

 `goods_brief` varchar(100) NOT NULL DEFAULT '',

 `goods_desc` text NOT NULL,

 `thumb_img` varchar(30) NOT NULL DEFAULT '',

 `goods_img` varchar(30) NOT NULL DEFAULT '',

 `ori_img` varchar(30) NOT NULL DEFAULT '',

 `is_on_sale` tinyint(4) NOT NULL DEFAULT '1',

 `is_delete` tinyint(4) NOT NULL DEFAULT '0',

 `is_best` tinyint(4) NOT NULL DEFAULT '0',

 `is_new` tinyint(4) NOT NULL DEFAULT '0',

 `is_hot` tinyint(4) NOT NULL DEFAULT '0',

 `add_time` int(10) unsigned NOT NULL DEFAULT '0',

 `last_update` int(10) unsigned NOT NULL DEFAULT '0',

 PRIMARY KEY (`goods_id`),

 UNIQUE KEY `goods_sn` (`goods_sn`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

引入模板goodsadd.php

define("ACC", true);

require("../include/init.php");

include(ROOT.'view/admin/templates/goodsadd.html');

goodsaddAct.php

<?php

define("ACC", true);

require("../include/init.php");

$data = array();

$data["goods_name"] = trim($_POST['goods_name']);

if ($data['goods_name'] =='') {

exit('商品名称不能为空');

}

/*

进行数据的检验

*/

$data["goods_sn"] = trim($_POST['goods_sn']);

$data["cat_id"] = trim($_POST['cat_id']);

$data["shop_price"] =  $_POST['shop_price'] ;

$data["goods_desc"] = trim($_POST['goods_desc']);

$data["goods_weight"] =  $_POST['goods_weight']* $_POST['weight_unit'];

$data["goods_number"] = trim($_POST['goods_number']);

$data["is_best"] =  isset($_POST['is_best'])?1:0;

$data["is_new"] = isset($_POST['is_new'])?1:0;

$data["is_hot"] = isset($_POST['is_hot'])?1:0;

$data["is_on_sale"] = isset($_POST['is_on_sale'])?1:0;

$data["goods_brief"] = trim($_POST['goods_brief']);

$data["add_time"] = time();

$goods = new GoodsModel();

if ($goods_id = $goods->add($data)) {

echo "商品发布成功";

}else{

echo "商品发布失败";

}

?>

GoodsModel.class.php

<?php

defined('ACC')||exit('ACC Deniec');

class GoodsModel extends Model{

protected $table = 'goods';

public function add($data){

return $this->db->autoExecute($this->table,$data);

}

}

?>

问题:

1 当字段多的时候,要逐一从$_POST里接受字段,比较麻烦

能否自动把$_POST里面合理的字段取出来。

即表中的列名对应$_POST字段

goods_id,goods_name,goods_desc 则会自动把$_POST里的字段拿出来

把表里的字段都拿出来,不合理的不要

2 CatModelGoodsModel 里,都有一个add方法,重复了,

如何解决,再抽象一步

3 字段的合法性,也自动判断