PHP連接數(shù)據(jù)庫實(shí)現(xiàn)注冊(cè)頁面的增刪改查操作
本文實(shí)例為大家分享了PHP連接數(shù)據(jù)庫實(shí)現(xiàn)注冊(cè)頁面的增刪改查操作的方法,供大家參考,具體內(nèi)容如下
1.連接數(shù)據(jù)庫
<?php //本地測(cè)試 $host = '127.0.0.1'; $port = 3306; $user = "root"; $pwd = ""; $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true); if(!$link) { die("Connect Server Failed: " . mysql_error()); } //選擇連接的數(shù)據(jù)庫庫名 mysql_select_db("my"); //設(shè)置字符編碼utf8 mysql_set_charset('utf8'); ?>
2.注冊(cè)頁面(html頁面)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>Document</title> </head> <body> <h3>注冊(cè)頁面</h3> <form action="add.php" method='post'> <table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'> <tr> <td align='right'>用戶名</td> <td><input type="text" name="username" id=""/>以小寫字母開始,長(zhǎng)度要求5~10</td> </tr> <tr> <td align='right'>密碼</td> <td><input type="password" name="password" id=""/>密碼不能為空</td> </tr> <tr> <td align='right'>郵箱</td> <td><input type="text" name="email" id="" /></td> </tr> <tr> <td align='right'>性別</td> <td> <input type="radio" name="sex" id="" value='1' />男 <input type="radio" name="sex" id="" value='2' />女 <input type="radio" name="sex" id="" value='3' />保密 </td> </tr> <tr> <td align='right'>個(gè)人簡(jiǎn)介</td> <td> <textarea name="txt" id="" cols="50" rows="10"></textarea> </td> </tr> <tr> <td colspan='2'><input type="submit" name='act' value='注冊(cè)' /></td> </tr> </table> </form> </body> </html>
3.將注冊(cè)數(shù)據(jù)顯示在數(shù)據(jù)庫
//往數(shù)據(jù)庫中添加數(shù)據(jù) <?php header("Content-type:text/html; charset=utf-8"); //-----------------------連接數(shù)據(jù)庫--------------------------- include_once "connect.php"; //-------------------------將數(shù)據(jù)連接到數(shù)據(jù)庫------------------ $time=time(); $sql="insert into user (username,password,email,sex,txt,`time`) value('{$_POST['username']}','{$_POST['password']}','{$_POST['email']}','{$_POST['sex']}','{$_POST['txt']}','{$time}')"; $res=mysql_query($sql); header("location:hello.php"); ?>
4.返回后臺(tái)界面
<?php header("Content-type:text/html; charset=utf-8"); //-----------------------連接數(shù)據(jù)庫------------------------------ include_once "connect.php"; //--------------------查詢數(shù)據(jù)庫-------------------------------- $query="select * from user"; $result=mysql_query($query); if(!$result) { die("could not to the database<br/>".mysql_error()); } //-------------------封裝函數(shù)----------------------------- //該函數(shù)將數(shù)據(jù)庫的數(shù)據(jù)寫成數(shù)組形式 function result2Arr($result){ while($result_row=mysql_fetch_assoc($result)){ $arr[] = $result_row; } return $arr; } $arr = result2Arr($result); foreach($arr as $key=>$value){ echo "<table border='1px'>"; echo "<table border='1px' >"; echo "<tr> "; echo "<td width='100px'>".$value['id']."</td>"; echo "<td width='100px'>".$value['username']."</td>"; echo "<td width='100px'>".$value['password']."</td>"; echo "<td width='200px'>".$value['email']."</td>"; echo "<td width='100px'>".$value['sex']."</td>"; echo "<td width='100px'>".$value['txt']."</td>"; echo "<td width='100px'>".date('Y-m-d H:i:s',$value['time'])."</td>"; echo "<td width='100px'><a href='update1.php?id=$value[id]'>修改</a> <a href='delete.php?id=$value[id]'>刪除</a></td>"; echo "<tr/>"; echo "</table>"; } ?>
5.修改數(shù)據(jù)
//當(dāng)用戶要修改信息時(shí),返回頁面,頁面中包含之前填寫的信息 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>Document</title> </head> <body> <div> <?php include_once "connect.php"; $sql="select * from user where id='".$_GET['id']."'"; //echo "sql:".$sql;(顯示出修改哪一行) $result=mysql_query($sql,$link); $arr = result2Arr($result); //print_r($arr); $row = $arr[0]; function result2Arr($result){ while($result_row=mysql_fetch_assoc($result)){ $arr[] = $result_row; } return $arr; } ?> <h3>注冊(cè)頁面</h3> <form action="update.php" method='post'> <input type="hidden" name="id" id="" value="<?php echo $row['id']?>"/> <table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'> <tr> <td align='right'>用戶名</td> <td><input type="text" name="username" id="" value="<?php echo $row['username']?>"/>以小寫字母開始,長(zhǎng)度要求5~10</td> </tr> <tr> <td align='right'>密碼</td> <td><input type="password" name="password" id=""value="<?php echo $row['password']?>"/>密碼不能為空</td> </tr> <tr> <td align='right'>郵箱</td> <td><input type="text" name="email" id="" value="<?php echo $row['email']?>"/></td> </tr> <tr> <td align='right'>性別</td> <td> <input type="radio" name="sex" id="" value='1' <?php if($row['sex']=='1'){ echo 'checked';}?>/>男 <input type="radio" name="sex" id="" value='2' <?php if($row['sex']=='2'){ echo 'checked';}?>/>女 <input type="radio" name="sex" id="" value='3' <?php if($row['sex']=='3'){ echo 'checked';}?>/>保密 </td> </tr> <tr> <td align='right'>個(gè)人簡(jiǎn)介</td> <td> <textarea name="txt" id="" cols="50" rows="10"><?php echo $row['txt']?></textarea> </td> </tr> <tr> <td colspan='2'><input type="submit" name='act' value='修改' /></td> </tr> </table> </form> </div> </body> </html>
//將修改的信息存入數(shù)據(jù)庫 <?php header("Content-type:text/html; charset=utf-8"); //通過post獲取頁面提交數(shù)據(jù)信息 $data = $_POST; //print_r($data); include_once "connect.php"; $sql = "update `user` set username='{$data['username']}',password='{$data['password']}', email='{$data['email']}',sex='{$data['sex']}',txt='{$data['txt']}' where id='{$data['id']}'"; echo $sql; $res = mysql_query($sql,$link); if($res){ header("Location:hello.php"); //echo "alert('修改成功')"; }else{ header("Location:update1.php?id=".$data['id']); //echo "alert('修改失敗')"; } ?>
6.刪除數(shù)據(jù)
//刪除數(shù)據(jù)庫里的數(shù)據(jù) <?php header("Content-type:text/html; charset=utf-8"); include_once 'connect.php'; $sql = "delete from user where id='".$_GET['id']."'"; $sus=mysql_query($sql,$link); if($sus){ header("location:hello.php"); }else{ echo "alert('刪除失敗')"; } ?> //若要?jiǎng)h除李四,點(diǎn)擊刪除后,會(huì)自動(dòng)跳轉(zhuǎn)到后臺(tái)頁面,數(shù)據(jù)庫里數(shù)據(jù)也刪除
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
php進(jìn)程(線程)通信基礎(chǔ)之System V共享內(nèi)存簡(jiǎn)單實(shí)例分析
這篇文章主要介紹了php進(jìn)程(線程)通信基礎(chǔ)之System V共享內(nèi)存,結(jié)合簡(jiǎn)單實(shí)例形式分析了PHP System V共享內(nèi)存原理、相關(guān)函數(shù)與基本使用技巧,需要的朋友可以參考下2019-11-11解決php的“It is not safe to rely on the system’s timezone setti
這篇文章主要介紹了解決php的“It is not safe to rely on the system’s timezone settings”問題的方法,需要的朋友可以參考下2015-10-10PHP連接及操作PostgreSQL數(shù)據(jù)庫的方法詳解
這篇文章主要介紹了PHP連接及操作PostgreSQL數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式分析了php針對(duì)PostgreSQL數(shù)據(jù)庫的基本連接以及增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01PHP之將POST數(shù)據(jù)轉(zhuǎn)化為字符串的實(shí)現(xiàn)代碼
今天來分享一個(gè)方便我們做LOG日志記錄的自定義函數(shù),需要將POST數(shù)據(jù)轉(zhuǎn)化為字符串,需要的朋友可以參考下2016-11-11PHP之a(chǎn)utoload運(yùn)行機(jī)制實(shí)例分析
這篇文章主要介紹了PHP的autoload運(yùn)行機(jī)制分析,從PHP源碼及應(yīng)用的角度對(duì)autoload進(jìn)行了較為深入的分析,需要的朋友可以參考下2014-08-08PHP nl2br函數(shù) 將換行字符轉(zhuǎn)成 <br>
PHP nl2br函數(shù) 將換行字符轉(zhuǎn)成 <br>,不是很了解的朋友可以參考下。2009-08-08