国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

怎么樣使用PHP連接數據庫實現注冊頁面的增刪改查操作

2020-03-22 17:51:10
字體:
來源:轉載
供稿:網友
  • 本文實例為大家分享了PHP連接數據庫實現注冊頁面的增刪改查操作的方法,供大家參考,具體內容如下
    1.連接數據庫
    php
    //本地測試
    $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());
    }
    //選擇連接的數據庫庫名
    mysql_select_db("my");
    //設置字符編碼utf8
    mysql_set_charset('utf8');
    ?>
    2.注冊頁面(html頁面)
    xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    Document
    注冊頁面
    action="add.php" method='post'>
    border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>

    align='right'>用戶名
    type="text" name="username" id=""/>以小寫字母開始,長度要求5~10


    align='right'>密碼
    type="password" name="password" id=""/>密碼不能為空


    align='right'>郵箱
    type="text" name="email" id="" />


    align='right'>性別

    type="radio" name="sex" id="" value='1' />男
    type="radio" name="sex" id="" value='2' />女
    type="radio" name="sex" id="" value='3' />保密



    align='right'>個人簡介

    name="txt" id="" cols="50" rows="10">



    colspan='2'> type="submit" name='act' value='注冊' />




    3.將注冊數據顯示在數據庫
    //往數據庫中添加數據
    php
    header("Content-type:text/html; charset=utf-8");
    //-----------------------連接數據庫---------------------------
    include_once "connect.php";
    //-------------------------將數據連接到數據庫------------------
    $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.返回后臺界面
    php
    header("Content-type:text/html; charset=utf-8");
    //-----------------------連接數據庫------------------------------
    include_once "connect.php";
    //--------------------查詢數據庫--------------------------------
    $query="select * from user";
    $result=mysql_query($query);
    if(!$result)
    {
    die("could not to the database".mysql_error());
    }
    //-------------------封裝函數-----------------------------
    //該函數將數據庫的數據寫成數組形式
    function result2Arr($result){
    while($result_row=mysql_fetch_assoc($result)){
    $arr[] = $result_row;
    }
    return $arr;
    }
    $arr = result2Arr($result);
    foreach($arr as $key=>$value){
    echo "";
    echo "";
    echo " ";
    echo "".$value['id']."";
    echo "".$value['username']."";
    echo "".$value['password']."";
    echo "".$value['email']."";
    echo "".$value['sex']."";
    echo "".$value['txt']."";
    echo "".date('Y-m-d H:i:s',$value['time'])."";
    echo "修改刪除";
    echo "";
    echo "";
    }
    ?>

    5.修改數據
    //當用戶要修改信息時,返回頁面,頁面中包含之前填寫的信息
    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">

    meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    Documenttitle>
    head>
    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>
    form action="update.php" method='post'>
    input type="hidden" name="id" id="" value=""/>
    table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>

    td align='right'>用戶名td>
    input type="text" name="username" id="" value=""/>以小寫字母開始,長度要求5~10td>
    tr>

    td align='right'>密碼td>
    input type="password" name="password" id=""value=""/>密碼不能為空td>
    tr>

    td align='right'>郵箱td>
    input type="text" name="email" id="" value=""/>td>
    tr>

    td align='right'>性別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>

    td align='right'>個人簡介td>

    textarea name="txt" id="" cols="50" rows="10">php echo $row['txt']?>textarea>
    td>
    tr>

    td colspan='2'>input type="submit" name='act' value='修改' />td>
    tr>
    table>
    form>
    div>
    body>
    html>

    //將修改的信息存入數據庫
    php
    header("Content-type:text/html; charset=utf-8");
    //通過post獲取頁面提交數據信息
    $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.刪除數據
    //刪除數據庫里的數據
    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('刪除失敗')";
    }
    ?>
    //若要刪除李四,點擊刪除后,會自動跳轉到后臺頁面,數據庫里數據也刪除

    以上就是本文的全部內容,希望對大家的學習有所幫助。PHP編程

    鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 循化| 铁岭县| 富民县| 常德市| 建始县| 台中县| 娱乐| 延边| 屏山县| 天气| 牙克石市| 宜川县| 衢州市| 新邵县| 鄂尔多斯市| 清涧县| 松江区| 湄潭县| 龙川县| 乾安县| 汾阳市| 嘉鱼县| 嵩明县| 聂拉木县| 开封市| 勐海县| 泸州市| 吴忠市| 东海县| 黄梅县| 宁德市| 大理市| 杭锦旗| 弥勒县| 高平市| 万山特区| 海南省| 林口县| 安顺市| 仙游县| 南城县|