Correction status:qualified
Teacher's comments:
数据库配置文件
<?php /*数据库配置文件*/ $db=[ 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'root', 'dbname' => 'stu', 'charset' => 'utf8', 'sql' => 3306, ];
点击 "运行实例" 按钮查看在线实例
数据库连接文件
<?php //导入数据库配置文件 require 'config.php'; //创建数据库连接 $mysqli = new mysqli($db['host'],$db['username'],$db['password']); //判断是否有错误信息 if($mysqli->connect_errno){ die('错误信息:').$mysqli->connect_errno.":".$mysqli->connect_error; } //选择要连接的数据库 $mysqli->select_db($db['dbname']); //选择默认字符集 $mysqli->set_charset($db['charset']);
点击 "运行实例" 按钮查看在线实例
添加的界面
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8" /> <title>添加</title> </head> <body> <form action="add.php" method="post"> <table cellspacing="0" border="1" width="400px" align="center"> <tr> <td>姓名:</td> <td><input type="text" name="name" id="" /></td> </tr> <tr> <td>性别:</td> <td><input type="text" name="sex" id="" /></td> </tr> <tr> <td>年龄:</td> <td><input type="text" name="age" id="" /></td> </tr> <tr> <td><input type="submit" value="提交" /></td> <td><input type="reset" value="重置" /></td> </tr> </table> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
mysqli处理添加
<?php if($_SERVER['REQUEST_METHOD']=='POST'){ //导入数据库连接 require ('sql.php'); //接受值 if(isset($_POST['name'])){ $name=$_POST['name']; } if(isset($_POST['sex'])){ $sex=$_POST['sex']; } if(isset($_POST['age'])){ $age=$_POST['age']; } //准备一个sql语句 $sql = "INSERT INTO `stuInfo` SET `name`= ?, `sex`= ? , `age` = ?"; //创建一个sql语句的预处理对象 $stmt = $mysqli->prepare($sql); //绑定参数 $stmt->bind_param('ssi',$name,$sex,$age); //执行sql语句 if($stmt->execute()){ //是否有数据新增 if($stmt -> affected_rows > 0){ //echo '成功新增了'.$stmt->affected_rows.'条数据'; echo "<script>alert('添加成功');location.href='select.php';</script>"; }else{ echo '没有新增数据'; } }else{ exit($stmt->errno.':'.$stmt->error); } }else{ die('类型错误'); }
点击 "运行实例" 按钮查看在线实例
mysqli的查询
<?php //导入数据库连接 require ('sql.php'); //准备sql语句 $sql="SELECT `id`,`name`,`sex`,`age` FROM `stuInfo` "; //初始化预处理对象 $stmt=$mysqli->stmt_init(); //执行预处理 if($stmt->prepare($sql)){ //执行sql语句 if($stmt->execute()){ //储存到结果集 $stmt->store_result(); //将结果集中的列绑定到变量上 $stmt->bind_result($id,$name,$sex,$age); //当结果集不为空的时候 if($stmt->num_rows>0){ // 循环遍历结果集 // fetch()每次获取一条记录,并将指针自动下移 echo '<table width="500px" align="center" border="1px" cellspacing="0">'; echo '<tr align="center">'; echo '<td>'; echo '姓名'; echo '</td>'; echo '<td>'; echo '性别'; echo '</td>'; echo '<td>'; echo '年龄'; echo '</td>'; echo '<td>'; echo '操作'; echo '</td>'; echo '</tr>'; while ($stmt->fetch()) { echo '<tr align="center">'; echo '<td>'; echo $name; echo '</td>'; echo '<td>'; echo $sex; echo '</td>'; echo '<td>'; echo $age; echo '</td>'; echo '<td>'; echo "<a href='edit.php?id={$id}'>修改</a> | <a href='delete.php?id={$id}' onclick='return confirm(\"是否删除\")'>删除</a>"; echo '</td>'; echo '</tr>'; } echo '</table>'; } }else{ die($stmt->errno.':'.$stmt->error); } }else{ die($stmt->errno.':'.$stmt->error); }
点击 "运行实例" 按钮查看在线实例
mysqli点击修改的页面
<?php //判断是否有数据传入 if($_GET){ //导入数据库连接 require ('sql.php'); //准备一个sql语句 //$sql="SELECT `id`,`name`,`sex`,`age` FROM `stuInfo` WHERE `id`=? ;"; $sql = "SELECT `id`,`name`,`sex`,`age` FROM `stuInfo` WHERE `id` = ? ;"; //初始化预处理对象 $stmt=$mysqli->stmt_init(); //执行预处理 if($stmt->prepare($sql)){ //绑定参数 $stmt->bind_param('i',$id); //接受数据 if(isset($_GET['id'])){ $id=$_GET['id']; } //执行预处理的sql语句 if($stmt->execute()){ //获取结果集 $stmt->store_result(); //将结果集中的列绑定到变量上 $stmt->bind_result($uid,$name,$sex,$age); //判断结果集是否为空 if($stmt->affected_rows>0){ //循环遍历结果集 //fetch指针每次下移一位 while($stmt->fetch()){ ?> <form action="edit_demo.php" method="post"> <table align="center" width="500px" border="1" cellspacing="0"> <input type="hidden" name="id" value="<?php echo $uid?>"> <tr> <td>姓名</td> <td><input type="text" name="name" id="" value="<?php echo $name;?>"></td> </tr> <tr> <td>性别</td> <td><input type="text" name="sex" id="" value="<?php echo $sex;?>"></td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" id="" value="<?php echo $age;?>"></td> </tr> <tr> <td><input type="submit" value="提交"></td> <td><input type="reset" value="重置"></td> </tr> </table> </form> <?php } }else{ echo '<p>数据为空</p>'; } }else{ die($stmt->errno.':'.$stmt->error); } }else{ die($stmt->errno.':'.$stmt->error); } }else{ exit('访问错误'); } ?>
点击 "运行实例" 按钮查看在线实例
mysqli处理修改
<?php //判断是否有提交 if($_SERVER['REQUEST_METHOD']=='POST'){ //导入数据库链接 require ('sql.php'); //准备一个sql语句 $sql="UPDATE `stuInfo` SET `name`=?,`sex`=?,`age`=? WHERE `id`=?"; //创建预处理对象 $stmt=$mysqli->prepare($sql); //接收参数 if(isset($_POST['name'])){ $name=$_POST['name']; } if(isset($_POST['sex'])){ $sex=$_POST['sex']; } if(isset($_POST['age'])){ $age=$_POST['age']; } if(isset($_POST['id'])){ $id=$_POST['id']; } //绑定参数 $stmt->bind_param('ssii',$name,$sex,$age,$id); //执行预处理的sql语句 if($stmt->execute()){ //检测是否有数据被修改 if($stmt->affected_rows>0){ echo "<script>alert('修改成功');location.href='select.php';</script>"; }else{ echo '<p>没有数据被修改</p>'; } }else{ exit($stmt->errno.':'.$stmt->error); } }else{ exit('404'); }
点击 "运行实例" 按钮查看在线实例
mysqli的删除
<?php /** *删除 */ if($_SERVER['REQUEST_METHOD']=='GET'){ //导入数据库连接 require ('sql.php'); if(isset($_GET['id'])){ $id=$_GET['id']; //echo $id; } //准备一个sql语句 $sql='DELETE FROM `stuInfo` WHERE `id` = ? ;'; //创建预处理对象 $stmt=$mysqli->prepare($sql); //绑定参数 $stmt->bind_param('i',$id); //执行预处理 if($stmt->execute()){ if($stmt->affected_rows>0){ echo '<script>alert("删除成功");location.href="select.php";</script>'; }else{ exit('没有数据被删除'); } }else{ exit($stmt->errno.':'.$stmt->error); } }else{ exit('404'); }
点击 "运行实例" 按钮查看在线实例
<!--------------------------------------------PDO------------------------------------------>
添加的界面
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8" /> <title>添加</title> </head> <body> <form action="add.php" method="post"> <table cellspacing="0" border="1" width="400px" align="center"> <tr> <td>姓名:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>性别:</td> <td><input type="text" name="sex" /></td> </tr> <tr> <td>年龄:</td> <td><input type="text" name="age" /></td> </tr> <tr> <td><input type="submit" value="提交" /></td> <td><input type="reset" value="重置" /></td> </tr> </table> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
数据库连接文件
<?php $dsn = 'mysql:host=127.0.0.1; dbname=stu'; $username='root'; $password='root'; //实例化PDO类 try{ $pdo= new PDO($dsn,$username,$password); //echo '连接成功'; }catch (PDOException $e){ die("Connect_Error:".$e->getMessage()); }
点击 "运行实例" 按钮查看在线实例
PDO添加
<?php /** *pdo添加 */ if($_SERVER['REQUEST_METHOD']=='POST'){ //导入数据库连接 require ('sql.php'); if(isset($_POST['name'])){ $name=$_POST['name']; } if(isset($_POST['sex'])){ $sex=$_POST['sex']; } if(isset($_POST['age'])){ $age=$_POST['age']; } //创建预处理 $stmt=$pdo->prepare("INSERT `stuInfo` SET `name`= :name , `sex`= :sex, `age`= :age"); if($stmt->execute(['name'=>$name,'sex'=>$sex,'age'=>$age])){ //echo '成功添加了'.$stmt->rowCount().'条数据'; echo "<script>alert('添加成功');location.href='select.php';</script>"; }else{ exit($stmt->errorInfo);//返回错误信息 } }else{ exit('404'); }
点击 "运行实例" 按钮查看在线实例
PDO查询
<?php /** *pdo查询 */ require ('sql.php'); //准备sql语句 $sql='SELECT * FROM `stuInfo`;'; //创建预处理对象 $stmt = $pdo->prepare($sql); //执行 $stmt->execute(); //循环取出数据 关联数组 //定义一个空数组 $rows=array(); while($row=$stmt->fetch(PDO::FETCH_ASSOC)){ $rows[]=$row;//存入二维数组 }; ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>查询</title> </head> <body> <table align="center" width="500" border="1px" cellspacing="0"> <tr align="center"> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>操作</td> </tr> <!-- 循环遍历 --> <?php foreach ($rows as $key=>$val){ ?> <tr align="center"> <td><?php echo $val['name'];?></td> <td><?php echo $val['sex'];?></td> <td><?php echo $val['age'];?></td> <td> <a href="edit.php?id=<?php echo $val['id'];?>">修改</a> | <a href="delete.php?id=<?php echo $val['id'];?>" onclick="return confirm('是否删除')">删除</a> </td> </tr> <?php }?> </table> </body> </html>
点击 "运行实例" 按钮查看在线实例
PDO修改的界面
<?php /** *pdo修改 */ if($_GET){ //导入数据库连接 require ('sql.php'); if(isset($_GET['id'])){ $id=$_GET['id']; } //准备一个sql语句 $sql='SELECT * FROM `stuInfo` WHERE `id`=:id;'; //创建预处理 $stmt=$pdo->prepare($sql); //执行预处理 if($stmt->execute(['id'=>$id])){ if($stmt->rowCount()>0){ //将结果返回一个关联数组 $row = $stmt->fetch(PDO::FETCH_ASSOC); } }else{ exit($stmt->errorInfo()); } }else{ exit('404'); } ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>修改</title> </head> <body> <form action="edit_demo.php" method="post"> <table align="center" width="500px" border="1" cellspacing="0"> <input type="hidden" name="id" value="<?php echo $row['id']?>"> <tr> <td>姓名</td> <td><input type="text" name="name" id="" value="<?php echo $row['name']?>"></td> </tr> <tr> <td>性别</td> <td><input type="text" name="sex" id="" value="<?php echo $row['sex'];?>"></td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" id="" value="<?php echo $row['age'];?>"></td> </tr> <tr> <td><input type="submit" value="提交"></td> <td><input type="reset" value="重置"></td> </tr> </table> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
PDO处理修改
<?php /** *pdo修改 */ if($_SERVER['REQUEST_METHOD']=='POST'){ //导入数据库连接 require ('sql.php'); if(isset($_POST['id'])){ $id=$_POST['id']; } if(isset($_POST['name'])){ $name=$_POST['name']; } if(isset($_POST['sex'])){ $sex=$_POST['sex']; } if(isset($_POST['age'])){ $age=$_POST['age']; } //准备sql语句 $sql = "UPDATE `stuInfo` SET `name`= :name,`sex`=:sex,`age`=:age WHERE `id`= :id"; //创建预处理 $stmt=$pdo->prepare($sql); //绑定参数 // $stmt->bindParam(':id',$id,PDO::PARAM_INT); // $stmt->bindParam(':name',$name,PDO::PARAM_STR); // $stmt->bindParam(':sex',$sex,PDO::PARAM_STR); // $stmt->bindParam(':age',$age,PDO::PARAM_INT); //执行 if($stmt->execute(['name'=>$name,'sex'=>$sex,'age'=>$age,'id'=>$id])){ if($stmt->rowCount()>0){ echo "<script>alert('修改成功');location.href='select.php';</script>"; }else{ exit('没有数据被修改'); } }else{ exit($stmt->errorInfo()); } }else{ exit('404'); }
点击 "运行实例" 按钮查看在线实例
PDO删除
<?php /** *pdo删除 */ if($_GET){ //导入数据库连接 require ('sql.php'); //接收id if(isset($_GET['id'])){ $id=$_GET['id']; } //准备一个sql语句 $sql='DELETE FROM `stuInfo` WHERE `id` =:id;'; //创建预处理对象 $stmt=$pdo->prepare($sql); //执行 if($stmt->execute(['id'=>$id])){ if($stmt->rowCount()>0){ echo '<script>alert("删除成功");location.href="select.php";</script>'; }else{ exit('没有数据被删除'); } }else{ exit($stmt->errorInfo()); } }else{ exit('404'); }
点击 "运行实例" 按钮查看在线实例