< ! 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>
< table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
< tr>
< td> 代号< / td>
< td> 姓名< / td>
< td> 性别< / td>
< td> 民族< / td>
< td> 生日< / td>
< td> 操作< / td>
< / tr>
<?php
$db = new MySQLi ( "localhost" , "root" , "123" , "mydb" ) ;
$sql = "select * from info" ;
$result = $db - > query ( $sql ) ;
$attr = $result - > fetch_all ( ) ;
foreach ( $attr as $v )
{
echo "<tr>" ;
$sex = $v [ 2 ] ? "男" : "女" ;
$name = NationName ( $v [ 3 ] ) ;
echo "<td>{ $v [ 0 ] } </td><td>{ $v [ 1 ] } </td><td>{ $sex } </td><td>{ $name } </td><td>{ $v [ 4 ] } </td><td><a href='shanchu.php?c={ $v [ 0 ] } ' οnclick=\"return confirm('确定删除么?')\">删除</a><a href='xiugai.php?c={ $v [ 0 ] } '>修改</a></td>" ;
echo "</tr>" ;
}
function NationName ( $code )
{
$db = new MySQLi ( "localhost" , "root" , "123" , "mydb" ) ;
$sql = "select name from nation where code='{ $code } '" ;
$result = $db - > query ( $sql ) ;
$attr = $result - > fetch_row ( ) ;
return $attr [ 0 ] ;
}
?>
< / table>
< a href= "add.php" > < input type= "button" value= "添加数据" / > < / a>
< / body>
< / html>
添加
< ! 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= "addchuli.php" method= "post" >
< div> 代号:< input type= "text" name= "code" / > < / div>
< div> 姓名:< input type= "text" name= "name" / > < / div>
< div> 性别:
< input type= "radio" name= "sex" value= "1" checked= "checked" / > 男
& nbsp;
< input type= "radio" name= "sex" value= "0" / > 女
< / div>
< div> 民族:
< select name= "nation" >
<?php
$db = new MySQLi ( "localhost" , "root" , "123" , "mydb" ) ;
$sql = "select * from nation" ;
$result = $db - > query ( $sql ) ;
$attr = $result - > fetch_all ( ) ;
foreach ( $attr as $v )
{
echo "<option value='{ $v [ 0 ] } '>{ $v [ 1 ] } </option>" ;
}
?>
< / select>
< / div>
< div> 生日:< input type= "text" name= "birthday" / > < / div>
< div> < input type= "submit" value= "添加" / > < / div>
< / form>
< / body>
< / html>
添加处理
<?php
$code = $_POST [ "code" ] ;
$name = $_POST [ "name" ] ;
$sex = $_POST [ "sex" ] ;
$nation = $_POST [ "nation" ] ;
$birthday = $_POST [ "birthday" ] ;
$db = new MySQLi ( "localhost" , "root" , "123" , "mydb" ) ;
$sql = "insert into info values('{ $code } ','{ $name } ',{ $sex } ,'{ $nation } ','{ $birthday } ')" ;
$db - > query ( $sql ) ;
header ( "location:main.php" ) ;
删除
<?php
$code = $_GET [ "c" ] ;
$db = new MySQLi ( "localhost" , "root" , "123" , "mydb" ) ;
$sql = "delete from info where code='{ $code } '" ;
$r = $db - > query ( $sql ) ;
if ( $r )
{
header ( "location:main.php" ) ;
}
else
{
echo "删除失败" ;
}
修改
< ! 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>
<?php
$code = $_GET [ "c" ] ;
$db = new MySQLi ( "localhost" , "root" , "123" , "mydb" ) ;
$sinfo = "select * from info where code='{ $code } '" ;
$rinfo = $db - > query ( $sinfo ) ;
$ainfo = $rinfo - > fetch_row ( ) ;
?>
< form action= "xiugaichuli.php" method= "post" >
< div> < input type= "hidden" name= "code" value= "<?php echo $ainfo [ 0 ] ; ?>" / > < / div>
< div> 姓名:< input type= "text" name= "name" value= "<?php echo $ainfo [ 1 ] ; ?>" / > < / div>
< div> 性别:
< input type= "radio" name= "sex" value= "1" <?php echo $ainfo [ 2 ] ? "checked='checked'" : "" ?> / > 男
& nbsp;
< input type= "radio" name= "sex" value= "0" <?php echo $ainfo [ 2 ] ? "" : "checked='checked'" ?> / > 女
< / div>
< div> 民族:
< select name= "nation" >
<?php
$sql = "select * from nation" ;
$result = $db - > query ( $sql ) ;
$attr = $result - > fetch_all ( ) ;
foreach ( $attr as $v )
{
if ( $v [ 0 ] == $ainfo [ 3 ] )
{
echo "<option selected='selected' value='{ $v [ 0 ] } '>{ $v [ 1 ] } </option>" ;
}
else
{
echo "<option value='{ $v [ 0 ] } '>{ $v [ 1 ] } </option>" ;
}
}
?>
< / select>
< / div>
< div> 生日:< input type= "text" name= "birthday" value= "<?php echo $ainfo [ 4 ] ; ?>" / > < / div>
< div> < input type= "submit" value= "修改" / > < / div>
< / form>
< / body>
< / html>
修改处理
<?php
$code = $_POST [ "code" ] ;
$name = $_POST [ "name" ] ;
$sex = $_POST [ "sex" ] ;
$nation = $_POST [ "nation" ] ;
$birthday = $_POST [ "birthday" ] ;
$db = new MySQLi ( "localhost" , "root" , "123" , "mydb" ) ;
$sql = "update info set name='{ $name } ',sex={ $sex } ,nation='{ $nation } ',birthday='{ $birthday } ' where code='{ $code } '" ;
$db - > query ( $sql ) ;
header ( "location:main.php" ) ;