学生管理信息
信息浏览
//连接数据库
try {
$pdo=new PDO("mysql:host=localhost;dbname=test;","root","562436");
} catch (Exception $e) {
die("数据库连接失败".$e->getMessage());
}
//执行SQL语句
$sql='select * from cms_stu';
foreach ($pdo->query($sql) as $row){
echo "
";
echo "
{$row['id']}";
echo "
{$row['name']}";
if($row['sex']=='m'){
echo "
男";
}else if($row['sex']=='w'){
echo "
女";
}
echo "
{$row['age']}";
echo "
{$row['classid']}班";
echo "
删除|修改";
echo "
";
}
?>
function doDel(id){
if(confirm('是否确定删除?')){
window.location='action.php?action=del&id='+id;
}
}
学生信息管理
浏览学生信息
增加学生信息
学生管理信息
增加学生
try {
$pdo=new PDO("mysql:host=localhost;dbname=test;","root","562436");
} catch (Exception $e) {
die("数据库连接失败".$e->getMessage());
}
//操作
switch ($_GET['action']){
case "add":
$name=$_POST['name'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$classid=$_POST['classid'];
$sql="insert into cms_stu values(null,'{$name}','{$sex}','{$age}','{$classid}')";
$rw=$pdo->exec($sql);
if($rw>0){
echo "";
}else{
echo "";
}
break;
case "del":
$id=$_GET['id'];
$sql="delete from cms_stu where id={$id}";
$pdo->exec($sql);
header("Location:index.php");
break;
case "edit":
$name=$_POST['name'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$classid=$_POST['classid'];
$id=$_POST['id'];
$sql="update cms_stu set name='{$name}',sex='{$sex}',age='{$age}',classid='{$classid}' where id={$id}";
$rw=$pdo->exec($sql);
if($rw>0){
echo "";
}else{
echo "";
}
break;
}
\
学生管理信息
try {
$pdo=new PDO("mysql:host=localhost;dbname=test;","root","562436");
} catch (Exception $e) {
die("数据库连接失败".$e->getMessage());
}
$sql="select * from cms_stu where id=".$_GET['id'];
$stmt=$pdo->query($sql);
if($stmt->rowCount() >0){
$stu=$stmt->fetch(PDO::FETCH_ASSOC);
}else{
die("没有修改");
}
?>
修改学生
CREATE TABLE `cms_stu` (
`id` mediumint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '',
`sex` char(2) NOT NULL COMMENT '男',
`age` int(11) NOT NULL,
`classid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;