增加数据
|
<?php namespace HomeController; use ThinkController; class IndexController extends Controller { public function add(){ $db = D( "Info" ); //1.使用数组方式 $arr = array ( "Code" => "p005" , "Name" => "aaa" , "Sex" =>1, "Nation" => "n003" , "Birthday" => "1999-02-03" ); $db ->add( $arr ); //2.使用映射的方式 $db ->Code = "P006" ; $db ->Name = "hgjgf" ; $db ->Sex = 1; $db ->Nation = "C004" ; $db ->Birthday = "1996-03-04" ; $db ->add(); } } |
自动收集表单的方法
首先在index文件夹中新建一个add文件
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head > < meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < title ></ title > </ head > < body > < form action="__ACTION__" method="post"> < div >代号:< input type="text" name="Code"/></ div > < br /> < div >姓名:< input type="text" name="Name"/></ div > < br /> < div >性别: < input type="radio" name="Sex" value="1"/>男 < input type="radio" name="Sex" value="0"/>女 </ div > < br /> < div >民族: < select name="Nation"> < foreach name="arr" item="v"> < option value="{$v.code}">{$v.name}</ option > </ foreach > </ select > </ div > < br /> < div >生日:< input type="text" name="Birthday"/></ div > < br /> < input type="submit" value="添加"/> </ form > </ body > </ html > |
<?php namespace HomeController; use ThinkController; class IndexController extends Controller { public function index(){; $this ->assign( "d" , "134212534" );; } public function add(){ $db = D( "Info" ); //3.自动收集表单 if ( empty ( $_POST )){ $arr = $db ->table( "Nation" ) ->select(); $this ->assign( "arr" , $arr ); $this ->show(); } else { $db ->create(); //收集表单 //$db->Sex = $_POST["Sex"]=="1"?trun:false;
$db ->add(); } } } |
========
修改数据
|
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head > < meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < title ></ title > </ head > < body > < form action="__ACTION__" method="post"> < div >< input type="text" name="Code" value="{$info.code}" hidden="hidden"/></ div > < div >姓名:< input type="text" name="Name" value="{$info.name}"/></ div > < div >性别: < if condition="$info.sex=='1'"> < input type="radio" name="Sex" value="1" checked="checked"/>男 < input type="radio" name="Sex" value="0"/>女 < else /> < input type="radio" name="Sex" value="1"/>男 < input type="radio" name="Sex" value="0" checked="checked" />女 </ if > </ div > < div >民族: < select name="Nation"> < foreach name="nation" item="v"> < if condition="$v['code']==$info['nation']"> < option value="{$v.code}" selected="selected" >{$v.name}</ option > < else /> < option value="{$v.code}" >{$v.name}</ option > </ if > </ foreach > </ select > </ div > < div >生日:< input type="text" name="Birthday" value="{$info.birthday}"/></ div > < input type="submit" value="修改"/> </ form > </ body > </ html > |
<?php namespace HomeController; use ThinkController; class IndexController extends Controller { public function index(){; $this ->assign( "d" , "134212534" );; } public function add(){ $db = D( "Info" ); //3.自动收集表单 if ( empty ( $_POST )){ $arr = $db ->table( "Nation" ) ->select(); $this ->assign( "arr" , $arr ); $this ->show(); } else { $db ->create(); //收集表单 $db ->add(); } } public function update(){ $db = D( "Info" ); $code = "p002" ; if ( empty ( $_POST )){ $nation = $db ->table( "Nation" ) ->select(); $info = $db ->find( $code ); $this ->assign( "info" , $info ); $this ->assign( "nation" , $nation ); $this ->show(); } else { $db ->create(); $db ->save(); } } } |
========
删除数据
<?php namespace HomeController; use ThinkController; class IndexController extends Controller { public function index(){; $this ->assign( "d" , "134212534" );; } public function add(){ $db = D( "Info" ); //3.自动收集表单 if ( empty ( $_POST )){ $arr = $db ->table( "Nation" ) ->select(); $this ->assign( "arr" , $arr ); $this ->show(); } else { $db ->create(); //收集表单 $db ->add(); } } public function update(){ $db = D( "Info" ); $code = "p002" ; if ( empty ( $_POST )){ $nation = $db ->table( "Nation" ) ->select(); $info = $db ->find( $code ); $this ->assign( "info" , $info ); $this ->assign( "nation" , $nation ); $this ->show(); } else { $db ->create(); $db ->save(); } } public function del(){ //$code = "p006"; $db = D( "Info" ); //$db->delete($code); $sql = "delete from info where code = 'p004'" ; $db ->execute( $sql ); //这里也适用于sql原生语句,增删改用execute 查用query } } |