赞
踩
(看的人似乎稍微多了点,我还是加个狗头吧0.0)
1.下载phpstudy2018
https://www.xp.cn/download.html
将其安装到D盘,然后启动MySQL和Apache服务
2.MySQL或者Apache无法启动
这些问题相信你能在网上找到解决办法,我当时也碰到过,但是通过搜索引擎都得到了解决
3.打开MySQL数据库并创建数据库和表
创建一个名为student的数据库
在数据库中创建两张数据表(分别为stu和stu_score)
如果你想直接使用文末的代码,那就跟我创建名字一样的数据库和数据表
如果你觉得我的命名不符合你的习惯,可以把数据库和数据表的名称改一下
(不过后面的代码,你就要把涉及到数据库和数据表名称的地方,都改过来)
登陆时报错,也请上百度/谷歌
4.把文末五个.php文件放入phpstudy的根目录
5.最终效果
在浏览器中输入127.0.0.1或者localhost就可以访问你的页面,实现web页面和MySQL的交互
反正我是一点报错都没有,不过就是页面写的太简单了,技术不错的同学可以找一些CSS样式加上去,若收藏的话,顺手也点个赞再走呗~
index.php
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>学生信息管理</title> <script> function doDel(id) { if (confirm("确定要删除么?")) { window.location = 'action.php?action=del&id='+id; } } </script> </head> <body> <center> <?php include_once "menu.php"; ?> <h3>浏览学生信息</h3> <table width="600" border="1"> <tr align="center"> <th>学号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>班级</th> <th>操作</th> </tr> <?php //1.连接数据库 $mysql_conf1 = array( 'host' => '127.0.0.1', 'db' => 'student', 'db_user' => 'root', 'db_pwd' => 'root' ); try { $pdo1 = new PDO("mysql:host=" . $mysql_conf1['host'] . ";dbname=" . $mysql_conf1['db'], $mysql_conf1['db_user'], $mysql_conf1['db_pwd']); } catch (PDOException $e) { die("数据库连接失败" . $e->getMessage()); } //2.解决中文乱码问题 $pdo1->query("SET NAMES 'UTF8'"); //3.执行sql语句,并实现解析和遍历 $sql1 = "SELECT * FROM stu "; foreach ($pdo1->query($sql1) as $row) { echo "<tr align='center'>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['name']}</td>"; echo "<td>{$row['sex']}</td>"; echo "<td>{$row['age']}</td>"; echo "<td>{$row['classid']}</td>"; echo "<td> <a href='javascript:doDel({$row['id']})'>删除</a> <a href='edit.php?id=({$row['id']})'>修改</a> </td>"; echo "</tr>"; } ?> </table> <br> <br> <br> <h3>浏览成绩信息</h3> <table width="600" border="1"> <tr align="center"> <th>学号</th> <th>姓名</th> <th>专业课成绩</th> <th>操作</th> </tr> <?php //1.连接数据库 $mysql_conf2 = array( 'host' => '127.0.0.1', 'db' => 'student', 'db_user' => 'root', 'db_pwd' => 'root' ); try { $pdo2 = new PDO("mysql:host=" . $mysql_conf2['host'] . ";dbname=" . $mysql_conf2['db'], $mysql_conf2['db_user'], $mysql_conf2['db_pwd']); } catch (PDOException $e) { die("数据库连接失败" . $e->getMessage()); } //2.解决中文乱码问题 $pdo2->query("SET NAMES 'UTF8'"); //3.执行sql语句,并实现解析和遍历 $sql2 = "SELECT * FROM stu_score "; foreach ($pdo2->query($sql2) as $row) { echo "<tr align='center'>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['name']}</td>"; echo "<td>{$row['score']}</td>"; echo "<td> <a href='javascript:doDel({$row['id']})'>删除</a> <a href='edit.php?id=({$row['id']})'>修改</a> </td>"; echo "</tr>"; } ?> </table> </center> </body> </html>
menu.php
<h2>学生信息管理</h2>
<a href="index.php">浏览学生</a>
<a href="add.php">增加学生</a>
<hr>
add.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>学生信息管理</title> </head> <body> <center> <?php include_once "menu.php"; ?> <h3>增加学生信息</h3> <form id="addstu" name="addstu" method="post" action="action.php?action=add"> <table> <tr> <td>学号</td> <td><input id="id" name="id" type="text"/></td> </tr> <tr> <td>姓名</td> <td><input id="name" name="name" type="text"/></td> </tr> <tr> <td>性别</td> <td><input type="radio" name="sex" value="男"/> 男 <input type="radio" name="sex" value="女"/> 女 </td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" id="age"/></td> </tr> <tr> <td>班级</td> <td><input id="classid" name="classid" type="text"/></td> </tr> <tr> <td>成绩</td> <td><input id="score" name="score" type="text"/></td> </tr> <tr> <td> </td> <td><input type="submit" value="增加"/> <input type="reset" value="重置"/> </td> </tr> </table> </form> </center> </body> </html>
edit.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>学生信息管理</title> </head> <body> <center> <?php include_once"menu.php"; //连接数据库 $mysql_conf = array( 'host' => '127.0.0.1', 'db' => 'student', 'db_user' => 'root', 'db_pwd' => 'root' ); try { $pdo = new PDO("mysql:host=" . $mysql_conf['host'] . ";dbname=" . $mysql_conf['db'], $mysql_conf['db_user'], $mysql_conf['db_pwd']); } catch (PDOException $e) { die("数据库连接失败" . $e->getMessage()); } $pdo->query("SET NAMES 'UTF8'"); //防止中文乱码 //拼接sql语句,取出信息 $sql1 = "SELECT * FROM stu WHERE id =".$_GET['id']; $sql2 = "SELECT * FROM stu_score WHERE id =".$_GET['id']; $stmt1 = $pdo->query($sql1); //返回预处理对象 $stmt2 = $pdo->query($sql2); //返回预处理对象 if($stmt1->rowCount()>0 && $stmt2->rowCount()>0){ $stu = $stmt1->fetch(PDO::FETCH_ASSOC); //按照关联数组进行解析 $stu_score = $stmt2->fetch(PDO::FETCH_ASSOC); //按照关联数组进行解析 }else{ die("没有要修改的数据!"); } ?> <form id="addstu" name="editstu" method="post" action="action.php?action=edit"> <input type="hidden" name="id" id="id" value="<?php echo $stu['id'];?>"/> <table> <tr> <td>学号</td> <td><input id="id" name="id" type="text" value="<?php echo $stu['id']?>"/></td> </tr> <tr> <td>姓名</td> <td><input id="name" name="name" type="text" value="<?php echo $stu['name']?>"/></td> </tr> <tr> <td>性别</td> <td><input type="radio" name="sex" value="男" <?php echo ($stu['sex']=="男")? "checked" : ""?>/> 男 <input type="radio" name="sex" value="女" <?php echo ($stu['sex']=="女")? "checked" : ""?>/> 女 </td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" id="age" value="<?php echo $stu['age']?>"/></td> </tr> <tr> <td>班级</td> <td><input id="classid" name="classid" type="text" value="<?php echo $stu['classid']?>"/></td> </tr> <tr> <td>成绩</td> <td><input id="score" name="score" type="text" value="<?php echo $stu_score['score']?>"/></td> </tr> <tr> <td> </td> <td><input type="submit" value="修改"/> <input type="reset" value="重置"/> </td> </tr> </table> </form> </center> </body> </html>
action.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>学生信息管理</title> </head> <body> <center> <?php //连接数据库 $mysql_conf = array( 'host' => '127.0.0.1', 'db' => 'student', 'db_user' => 'root', 'db_pwd' => 'root' ); try { $pdo = new PDO("mysql:host=" . $mysql_conf['host'] . ";dbname=" . $mysql_conf['db'], $mysql_conf['db_user'], $mysql_conf['db_pwd']); } catch (PDOException $e) { die("数据库连接失败" . $e->getMessage()); } $pdo->query("SET NAMES 'UTF8'"); //防止中文乱码 $id = $_POST['id']; $name = $_POST['name']; $sex = $_POST['sex']; $age = $_POST['age']; $classid = $_POST['classid']; $score = $_POST['score']; //通过action的值进行对应操作 switch ($_GET['action']) { case 'add':{ //增加操作 $sql1 = "INSERT INTO stu VALUES ('{$id}', '{$name}', '{$sex}', '{$age}', '{$classid}')"; $sql2 = "INSERT INTO stu_score VALUES ('{$id}', '{$name}', '{$score}')"; $rw1 = $pdo->exec($sql1); $rw2 = $pdo->exec($sql2); if ($rw1>0 && $rw2>0) { echo "<script> alert('增加成功'); window.location='index.php'; //跳转到首页 </script>"; } else { echo "<script> alert('增加失败'); window.history.back(); //返回上一页 </script>"; } break; } case "del": { //获取表单信息 $id = $_GET['id']; $sql3 = "DELETE FROM stu WHERE id={$id}"; $sql4 = "DELETE FROM stu_score WHERE id={$id}"; $pdo->exec($sql3); $pdo->exec($sql4); header("Location:index.php"); //跳转到首页 break; } case "edit" :{ //获取表单信息 $name = $_POST['name']; $sex = $_POST['sex']; $age = $_POST['age']; $classid = $_POST['classid']; $score = $_POST['score']; $sql5 = "UPDATE stu SET name='{$name}',sex='{$sex}',age='{$age}',classid='{$classid}' WHERE id='{$id}'"; $sql6 = "UPDATE stu_score SET score='{$score}' WHERE id='{$id}'"; $rw3 = $pdo->exec($sql5); $rw4 = $pdo->exec($sql6); if($rw3>0 || $rw4>0){ echo "<script>alert('修改成功');window.location='index.php'</script>"; }else{ echo "<script>alert('修改失败');window.history.back()</script>"; } break; } } ?> </center> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。