1. 程式人生 > >PHP初學者自寫學生管理系統

PHP初學者自寫學生管理系統

初寫 學生管理系統(初學者記錄)
1.後臺登陸頁面
<?php    

try {
	$o="mysql:host=127.0.0.1;dbname=testx";
	$username="root";
	$password="root";
	$pdo=new pdo($o,$username,$password);


	$sql="select * from admin";
	// echo $sql;
	$res=$pdo->query($sql);

	// var_dump($res);
	// die;
} catch (Exception $e) {
	echo $e->getmessage();
}
if (isset($_POST)&& !empty($_POST)) {
	foreach ($res as $k => $v) {
		if ($_POST["username"]==$v["user"] && $_POST["password"]==$v["password"]) {
			header("location:http://www.azey.com/xt/dl.php");
		}
	}
}

?>

 <!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>後臺登陸系統</title>
</head>
<body>
	<center>
	<form method="post" type="none" action="">
			<ul type="none" style="margin-top: 200px;">
				<li>
					<label>Id:</label>
					<input type="text" name="username">
				</li>
				<li style="margin-top: 10px;">
					<label>Password:</label>
					<input type="password" name="password" style="margin-left: 12px;mar">
				</li>
				<li>
					<label> </label>
					<input type="submit" name="login" value="登入" style="margin-left: 50px;margin-top: 20px;">
				</li>		
			</ul>
	</form>
	</center>
</body>
</html>
2.主頁面
<?php

     include_once "Gn.php";

try {
	$o="mysql:host=127.0.0.1;dbname=testx";
	$username="root";
	$password="root";
	$pdo=new pdo($o,$username,$password);

	$sql="select * from lb";
	// echo $sql;
	$res=$pdo->query($sql);
	// var_dump($res);
} catch (Exception $e) {
	echo $e->getmessage();
}

    ?>

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<head>
<title>Student information management system</title>	
</head>
<body bgcolor="#169FE9">
<center>
     <h3>PHP1804 student information browsing</h3>
     <table width="600" border="1">
             <th>ID</th>
             <th>Name</th>
             <th>Sex</th> 
             <th>Age</th>
             <th>Class</th>
             <th>add & del</th>
             <?php foreach ($res as $k => $v){?>
             	<tr style="text-align: center;">
					<td><?php echo $v['id'] ?></td>
					<td><?php echo $v['name'] ?></td>
					<td><?php echo $v['sex'] ?></td>
					<td><?php echo $v['age'] ?></td>
             		<td><?php echo $v['class'] ?></td>
             		<td>
                     <a href="http://www.azey.com/po/hasaki3.php?id='<?php echo $v["id"] ?>'">Delete</a>
                     <br>
                     <a href="hasaki5.php?id='<?php echo $v["id"] ?>'">Modify</a>
             		</td>
             	</tr>
             <?php }?>
</center>
</body>
</html>

3.新增
<?php 
error_reporting(0);

try {
	$o="mysql:host=127.0.0.1;dbname=testx";
	$username="root";
	$password="root";
	$pdo=new pdo($o,$username,$password);

	$sql="insert into Lb (name,sex,age,class) values ('".$_POST['name']."',".$_POST['sex'].",".$_POST['age'].",".$_POST['class'].")";
	// echo $sql;
	$res=$pdo->query($sql);
	// var_dump($res);
} catch (Exception $e) {
	echo $e->getmessage();
}



 ?>

<html>
<head>
    <title>Student information management</title>
</head>
<body bgcolor="#169FE9">
<center>
    <?php include_once "Gn.php"; ?>
    <h3>Add student information</h3>

    <form id="addstu" name="addstu" method="post" action="">
        <table>
            <tr>
                <td>Name</td>
                <td><input id="name" name="name" type="text"/></td>

            </tr>
            <tr>
                <td>Sex</td>
                <td><input type="radio" name="sex" value="1"/>&nbsp;Man
                    <input type="radio" name="sex" value="2"/>&nbsp;Woman
                </td>
            </tr>
            <tr>
                <td>Age</td>
                <td><input type="text" name="age" id="age"/></td>
            </tr>
            <tr>
                <td>Class</td>
                <td><input id="class" name="class" type="text"/></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="提交"/>
                    <input type="reset" value="重置"/>
                </td>
            </tr>
        </table>

    </form>
</center>
</body>
</html>
4.刪除
<?php 

try {
	$o="mysql:host=127.0.0.1;dbname=testx";
	$username="root";
	$password="root";
	$pdo=new pdo($o,$username,$password);

	$sql="delete from lb where id=".$_GET['id'];
	// echo $sql;
	$res=$pdo->query($sql);
	// var_dump($res);
} catch (Exception $e) {
	echo $e->getmessage();
}

 ?>
5.修改
<?php 
try {
	$o="mysql:host=127.0.0.1;dbname=testx";
	$username="root";
	$password="root";
	$pdo=new pdo($o,$username,$password);

	$sql = "update Lb set name = '".$_POST['name']."',sex=".$_POST['sex'].",age=".$_POST['age'].",class=".$_POST['class']." where id=".$_GET['id'];
	// echo $sql;
	$res=$pdo->query($sql);
	// var_dump($res);
} catch (Exception $e) {
	echo $e->getmessage();
}
   header("location:http://www.azey.com/xt/dl.php");
 ?>



 <center>
 <?php
     include_once "Gn.php";
     ?>
 </center>
 <!DOCTYPE html>
 <html>
 <head>
 	<title>Student information management system</title>
 	<meta charset="utf-8">
 </head>
 <body bgcolor="#169FE9">
 	<center>
   <form action="" method="post" enctype="multipart/form-data">
     <table>
            <tr>
                <td>Name</td>
                <td><input id="name" name="name"  type="text" value=" <?php echo $rre['name'] ?> " /></td>

            </tr>
            <tr>
               <?php  if ($rre['sex']==1) {	?>
               	
               	<td>Sex</td>
                <td><input type="radio" name="sex" value="1"checked/>&nbsp;Man
                    <input type="radio" name="sex" value="2"/>&nbsp;Woman
                </td>

               <?php }else if($rre['sex']==2) { ?>
               
				<td>Sex</td>
                <td><input type="radio" name="sex" value="1"/>&nbsp;Man
                    <input type="radio" name="sex" value="2"checked/>&nbsp;Woman
                </td>

            </tr>
			<?php } ?>

            <tr>
                <td>Age</td>
                <td><input type="text" name="age" id="age" value="<?php echo $rre['age']; ?>"/></td>
            </tr>
            <tr>           	
                <td>Class</td>
                <td><input id="class" name="class" type="text" value="<?php echo $rre['class']; ?>"/></td>
            </tr>            
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="提交"/>
                    <input type="reset" value="重置"/>
                </td>
            </tr>
        </table>
   </form>
   </center>
 </body>
 </html>