<?php // php鏈接數據庫 /** PHP想操做mysql,得先鏈接上Mysql服務器 1:鏈接須要哪些要素? 答:服務器的地址,用戶名,密碼,端口 2:怎麼連 答:用mysql_connect()函數來連 */ /* $server: 服務器的地址[域名/ip],不寫通常默認是localhost $username: 帳號 $password: 密碼 $new_link: 是否從新鏈接 */ $conn=mysql_connect('localhost','root','1987',true); if(!$conn){ die('鏈接錯誤'.mysql_error()); } mysql_select_db('php',$conn); mysql_set_charset('utf8'); $sql='select * from stu'; $res=mysql_query($sql); while (!!$row=mysql_fetch_assoc($res)){ $arr[]=$row; } //print_r($arr); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>學生信息</title> <pre> 接下來,咱們想達到以下 效果: 點擊誰,就修改誰. 具體:點擊33號用戶,則來到一張表單頁面, 表單的值 正好是33號用戶的信息. 提交後,則修改33號用戶 思路: 點擊N號時,把N做爲參數傳過去. 根據參數拼接sql語句,來查詢N號用戶 查出來的結果,做爲表單的默認值. </pre> <body> <form action="add.php" method="post"> <table border='1px'> <center><caption> 學生成績表</caption></center> <tr><td>stu_id</td><td>stu_name</td><td>stu_course</td><td>grade</td><td></td><td></td><td></td></tr> <?php foreach ($arr as $v){?> <tr><td><?php echo $v['stu_id']?></td><td><?php echo $v['stu_name']?></td><td><?php echo $v['stu_course']?></td><td><?php echo $v['grade']?></td><td><a href='update.php?id=<?php echo $v['stu_id']?>'>修改</a></td> <td><a href='del.php?id=<?php echo $v['stu_id']?>'>刪除</a></td></tr> <?php }?> </table> <input type="submit" value='添加學生'/> </form> </body> </html> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>學生信息</title> </head> <body> <form action="controller.php" method="post"><br/> stu_name<input type="text" name='stuName'/><br/> stu_course<input type="text" name='stuCourse'/> <br/> grade<input type="text" name='grade'/> <br/> <input type="submit" value='提交'/> </form> </body> </html> <?php $conn=mysql_connect('localhost','root','1987'); if (!$conn){ die('鏈接錯誤'.mysql_error()); } mysql_select_db('php',$conn); mysql_set_charset('utf8'); if (!empty($_POST['stuName'])){ $stu_name=addslashes($_POST['stuName']); } if (!empty($_POST['stuCourse'])){ $stu_course=addslashes($_POST['stuCourse']); } if (!empty($_POST['grade'])){ $grade=addslashes($_POST['grade']); } $sql="insert into stu(stu_name,stu_course,grade)values('$stu_name','$stu_course',$grade)"; //echo $sql;exit; $b= mysql_query($sql); if(!$b){ echo '添加失敗'; }else{ if(mysql_affected_rows($conn)>0){ echo '添加成功'; }else{ echo '沒有行受影響'; } } //關閉資源 mysql_free_result($b); mysql_close($conn); echo "<a href='stulist.php'>返回列表</a>"; ?> <?php $conn=mysql_connect('localhost','root','1987'); if (!$conn){ die('鏈接錯誤'.mysql_error()); } mysql_select_db('php',$conn); mysql_set_charset('utf8'); if (!empty($_POST['stuName'])){ $stu_name=addslashes($_POST['stuName']); } if (!empty($_POST['stuCourse'])){ $stu_course=addslashes($_POST['stuCourse']); } if (!empty($_POST['grade'])){ $grade=addslashes($_POST['grade']); } if (!empty($_POST['id'])){ $id=addslashes($_POST['id']); } $sql="update stu set stu_name='".$stu_name."' ,stu_course='".$stu_course."' ,grade=".$grade.' where stu_id='.$id; // echo $sql;exit; $b= mysql_query($sql); if(!$b){ echo '修改失敗'; }else{ if(mysql_affected_rows($conn)>0){ echo '修改爲功'; }else{ echo '沒有行受影響'; } } //關閉鏈接 mysql_close($conn); echo "<a href='stulist.php'>返回列表</a>"; ?> <?php $conn=mysql_connect('localhost','root','1987'); if (!$conn){ die('鏈接錯誤'.mysql_error()); } mysql_select_db('php',$conn); mysql_set_charset('utf8'); $stu_id=isset($_GET['id'])?$_GET['id']+0:0; $sql='delete from stu where stu_id='.$stu_id; $b= mysql_query($sql); if(!$b){ echo '刪除失敗'; }else{ if(mysql_affected_rows($conn)>0){ echo '刪除成功'; }else{ echo '沒有行受影響'; } } //關閉鏈接 mysql_close($conn); echo "<a href='stulist.php'>返回列表</a>"; ?> <?php $conn=mysql_connect('localhost','root','1987'); if (!$conn){ die('鏈接錯誤'.mysql_error()); } mysql_select_db('php',$conn); mysql_set_charset('utf8'); $stu_id=isset($_GET['id'])?$_GET['id']+0:0; //到數據庫查詢學生信息 $sql='select * from stu where stu_id='.$stu_id; //echo $sql;exit; $res=mysql_query($sql); $row=mysql_fetch_assoc($res); //這裏只有一條數據,就不用while循環了 ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>學生信息</title> </head> <body> <form action="controller1.php" method="post"> stu_id<input type="hidden" name='id' value='<?php echo $row['stu_id'];?>'/><br/> stu_name<input type="text" name='stuName' value='<?php echo $row['stu_name'];?>'/><br/> stu_course<input type="text" name='stuCourse' value="<?php echo $row['stu_course'];?>"/> <br/> grade<input type="text" name='grade' value='<?php echo $row['grade'];?>'/> <br/> <input type="submit" value='提交'/> </form> </body> </html> 由此可知面向過程使代碼量大