您的位置:首页 > 编程语言 > PHP开发

(边写边更)用PHP简单的学生个人信息记录程序

2016-03-19 23:03 791 查看
史老师开学前布置的项目,用网页实现学生个人信息的增删查改,寒假时只看完了html和js,jQuery和PHP都处于现学现卖的状态,不过既然没要求交,那就瞎折腾吧。

(2016.3.19)目前已实现功能:

1.增:通关网页页面(表单)向数据库中添加个人信息

2.删:清空表单信息、删除数据表(后者慎用)

3.查:通过班级/姓名/学号/性别查询表单信息(存在BUG)

主要结构为三个主要页面和五个PHP文件:

1.主页界面Indx,供用户选择使用的功能(信息录入or信息查询)

源代码如下

<html>
<head>
<title>学生个人信息登记表</title>
</head>
<body>
<h1 align="center" >学生个人信息登记表</h1>
<a href="PHPtest.php"><p align="center">个人信息录入</p>
<a href="Search.php"><p align="center">个人信息查询</p>
</body>
</html>
界面如图,之后可逐步添加其他功能并进行页面美化



2..个人信息录入界面PHPtest.php,主要实现数据表的添加和清空、删除功能

(1)添加:在文本框里输入的个人信息在点击submit后,通过post方法发送至数据库,再通过MySQL的Insert方法添加至数据表

(2)清空数据表内容:点击最下方的清空表单按钮,会利用HTTP POS通过Ajax发送post请求,调用emptyform.php文件,在文件中DELETE FROM语句实现数据表清空

(3)删除数据表(慎用):点击最下方的删除表单按钮,会利用HTTP POS通过Ajax发送post请求,调用deleteform.php文件,在文件中DROP TABLE语句实现数据表清空(删除后数据表无法使用,需重建)

(4)获取整个表单的信息:点击最下方的获取表单按钮,同理调用getdata.php,使用SELECT语句输出表单。

源代码如下

(1)PHPtest.php

<!DOCTYPE HTML>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$.post(
'getdata.php',
function(data){
$("#div2").html(data);
}
);
});

$("#btn2").click(function(){
var ans=confirm("确定清空整个表单?");
if(ans){
$.post(
'emptyform.php',
function(data){
$("#div2").html(data);
}
);
}
});

$("#btn3").click(function(){
var ans=confirm("确定删除整个表单?");
if(ans){
$.post(
'deleteform.php',
function(data){
$("#div2").html(data);
}
);
}
});

/*$("#submit").submit(function(){
$.post(
"insert.php",
function(data){
$("#div1").html(data);
}
);
});*/
});
</script>
</head>
<body>
<?php
// define variables and set to empty values
$class = $id = $gender = $name= "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$class = test_input($_POST["class"]);
$id = test_input($_POST["id"]);
$name = test_input($_POST["name"]);
$gender = test_input($_POST["gender"]);
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>学生个人信息录入表</h2>
<form id="form1" method="post" action="insert.php">
班级:<input type="text" name="class">
<br><br>
学号:<input type="text" name="id">
<br><br>
姓名:<input type="text" name="name">
<br><br>
性别:
<input type="radio" name="gender" value="女">女性
<input type="radio" name="gender" value="男">男性
<br><br>
<input type="submit" id="submit" name="submit" value="提交">
<input type="button" id="btn1" name="getdata" value="获取表单">
<input type="button" id="btn2" name="delete" value="清空表单">
<input type="button" id="btn3" name="empty" value="删除表单">
</form>

<div id="div1"></div>
</br>
<div id="div2"></div>
</body>
</html>


(2)emptyform.php

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql = "DELETE FROM student";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "成功清空数据表";
mysql_close($con);
?>


(3)deleteform.php

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql = "DROP TABLE student";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "成功删除数据表";
mysql_close($con);
?>


(4)getdata.php

<?php
$con1 = mysql_connect("localhost","root","");
if (!$con1)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con1);

$result = mysql_query("SELECT * FROM student");

echo "<table border='1'>
<tr>
<th>班级</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con1);
?>


界面如图,同样可逐步修改现有功能,或添加其他功能并进行页面美化。



3.学生个人信息查询界面Search.php,可通过班级/姓名/学号/性别查询表单信息(存在BUG)

点击四个按钮中的任意一个按钮,其他按钮会自动隐藏,按钮下显示出一个文本框,在文本框中输入的个人信息在点击submit后,通过post方法发送至数据库,在searchdaya.php中再通过MySQL的ELECT * FROM table WHERE语句实现查找

源代码如下:

(1)Search.php

<html>
<head>
<title>学生个人信息查询表</title>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
//document.getElementById("div1").innerHTML="<input type='text' id='text1'>";
$("#div1").html("<input type='text' name='class' id='text1'>");
$("#btn2").hide();$("#btn3").hide();$("#btn4").hide();
});
$("#btn2").click(function(){
//document.getElementById("div2").innerHTML="<input type='text' id='text2'>";
$("#div2").html("<input type='text' name='id' id='text2'>");
$("#btn1").hide();$("#btn3").hide();$("#btn4").hide();
});
$("#btn3").click(function(){
//document.getElementById("div3").innerHTML="<input type='text' id='text3'>";
$("#div3").html("<input type='text' name='name' id='text3'>");
$("#btn1").hide();$("#btn2").hide();$("#btn4").hide();
});
$("#btn4").click(function(){
$("#div4").html("<input type='radio' name='gender' value='女'>女性 <input type='radio' name='gender' value='男'>男性");
$("#btn1").hide();$("#btn2").hide();$("#btn3").hide();
});
});
</script>
</head>
<body>

<?php
// define variables and set to empty values
$class = $id = $gender = $name= "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$class = test_input($_POST["class"]);
$id = test_input($_POST["id"]);
$name = test_input($_POST["name"]);
$gender = test_input($_POST["gender"]);
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>学生个人信息查询表</h2>
<form id="form2" method="post" action="searchdata.php">
<input type="button" id="btn1" name="class" value="按班级查询"><div id="div1"></div>
</br></br>
<input type="button" id="btn2" name="id" value="按学号查询"><div id="div2"></div>
</br></br>
<input type="button" id="btn3" name="name" value="按姓名查询"><div id="div3"></div>
</br></br>
<input type="button" id="btn4" name="gender" value="按性别查询"><div id="div4"></div>
</br></br>
<input type="submit" id="submit" name="submit" value="提交查询"><div id="div5"></div>
</form>

<div id="div1"></div>
</br>
<div id="div2"></div>
</body>
</html>


(2)searchdata.php

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);

if(mysql_query("SELECT * FROM student WHERE class='$_POST[class]'")!="")
{
$result = mysql_query("SELECT * FROM student
WHERE class='$_POST[class]'");

echo "<table border='1'>
<tr>
<th>班级</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
}

if(mysql_query("SELECT * FROM student WHERE id='$_POST[id]'")!="")
{
$result = mysql_query("SELECT * FROM student
WHERE id='$_POST[id]'");

echo "<table border='1'>
<tr>
<th>班级</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
}

if(mysql_query("SELECT * FROM student WHERE name='$_POST[name]'")!="")
{
$result = mysql_query("SELECT * FROM student WHERE name='$_POST[name]'");

echo "<table border='1'>
<tr>
<th>班级</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
}

if(mysql_query("SELECT * FROM student WHERE gender='$_POST[gender]'")!="")
{
$result = mysql_query("SELECT * FROM student WHERE gender='$_POST[gender]'");

echo "<table border='1'>
<tr>
<th>班级</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>


界面如图


目前查询功能仍然存在BUG,需要改进。

(待更新)

3.20更新

新增个人信息修改页面,将之前页面中的清空表单、删除表单功能移动至该页面(Modifyform.php),同时增加表单按学号排序功能



源代码如下:

Modifyform.php

<!DOCTYPE HTML>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){

$("#btn1").click(function(){
$.post(
'order.php',
function(data){
$("#div1").html(data);
}
);
});

$("#btn2").click(function(){
var ans=confirm("确定清空整个表单?");
if(ans){
$.post(
'emptyform.php',
function(data){
$("#div1").html(data);
}
);
}
});

$("#btn3").click(function(){
var ans=confirm("确定删除整个表单?");
if(ans){
$.post(
'deleteform.php',
function(data){
$("#div1").html(data);
}
);
}
});
});
</script>
</head>
<body>

<?php
// define variables and set to empty values
$class = $id = $gender = $name= "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$class = test_input($_POST["class"]);
$id = test_input($_POST["id"]);
$name = test_input($_POST["name"]);
$gender = test_input($_POST["gender"]);
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>学生个人信息表修改</h2>
<form id="form1" method="post">
<input type="button" id="btn1" name="order" value="按学号排序">
</br>
<input type="button" id="btn2" name="delete" value="清空表单">
</br>
<input type="button" id="btn3" name="empty" value="删除表单">
</form>
<div id="div1"></div>
</body>
</html>


实现排序功能的order.php

<?php
$con1 = mysql_connect("localhost","root","");
if (!$con1)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con1);
$result = mysql_query("SELECT * FROM student ORDER BY id");

echo"排序后结果如下:"."</br>";
echo "<table border='1'>
<tr>
<th>班级</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con1);
?>


计划明天(3.22)修改个人信息查询界面,按钮查询着实不太美观,应该改成其他排版格式(比如option)

3.22日更新

对查询界面Serach.php做出了修改,将button改为了option,既减少了代码量,又使界面更加简洁

同时searchdata.php也作出相应改动,使用searchtype和searchterm进行数据库查询而不是直接用变量名,从而去除了之前的BUG

代码如下:

(1)Sreach.php:

<html>
<head>
<title>学生个人信息表查询</title>
</head>

<body>
<h1>学生个人信息表查询</h1>

<form action="searchdata(2).php" method="post">
选择查询类型:<br />
<select name="searchtype">
<option name="class" value="class">班级
<option name="id" value="id">学号
<option name="name" value="name">姓名
<option name="gender" value="gender">性别
</select>
<br />
输入查询关键字:<br />
<input name="searchterm" type="text" size="40">
<br />
<input type="submit" name="submit" value="Search">
</form>

</body>
</html>


(2)searchdata.php

<html>
<head>
<title>学生个人信息表查询结果</title>
</head>
<body>
<h1>学生个人信息表查询结果</h1>
<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);

if (!$searchtype || !$searchterm) {
echo 'You have not entered search details.  Please go back and try again.';
exit;
}

if (!get_magic_quotes_gpc()){
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}

$con1 = mysql_connect("localhost","root","");
if (!$con1)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con1);
$result = mysql_query("SELECT * FROM student WHERE ".$searchtype." like '%".$searchterm."%'");
$num_results=mysql_num_rows($result);
echo "共有".$num_results."个结果:";
echo "<table border='1'>
<tr>
<th>班级</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con1);
?>
</body>
</html>


至此,所有主要功能都已大致完成,之后将逐步进行修改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: