利用PHP實現登入與註冊功能以及使用PHP讀取mysql資料庫——以表格形式顯示資料

來源:互聯網
上載者:User

標籤:eth   mil   from   int   strong   var   system   use   echo   

登入介面

<body>
<form action="login1.php" method="post">
<div>使用者名稱:<input type="text" name="uid" /></div><br />
<div>密碼:<input type="password" name="pwd" /></div><br />
<div><input type="submit" value="登入"/></div>
</form>
</body>

後台登入處理

<?php
$uid = $_POST["uid"];
$pwd = $_POST["pwd"];

$db = new MySQLi("localhost","root","","0710_info");
if(mysqli_connect_error()){
    die("串連失敗");    
}
$sql = "select pwd from users where uid=‘{$uid}‘";
//echo $sql;
$result = $db->query($sql);
$arr = $result->fetch_row();

//防止sql注入攻擊
if($arr[0]==$pwd && !empty($pwd)){
    //跳轉到主介面(php方式)
    header("location:register_system.php");
    //JS跳轉頁面方式
    /*echo "<script>
            window.location.href=‘register_system.php‘
          </script>";*/
}else{
    header("location:login_system.php");
}

?>

註冊介面

<body>
<form action="register1.php" method="post" >
    <div style="font-size:20px; font-weight:1000; font-family:微軟雅黑; margin-left:50px; margin-bottom:20px">使用者註冊</div>
    <div>使用者名稱:<input type="text" name="uid" /></div><br />
    <div>登入密碼:<input type="password" name="pwd" /></div><br />
    <div>姓名:<input type="text" name="name" /></div><br />
    <div>性別:
        <input type="radio" name="sex" value="1" />男
        <input type="radio" name="sex" value="0" />女
    </div><br />
    <div><input type="submit" value="註冊" onclick="return check()"/></div>
</form>
</body>
JS表單驗證
<script>
function check(){
    var uid = document.getElementsByTagName("input")[0].value;
    if(uid == ""){
        alert("請輸入使用者名稱!");
        return false;
    }
    var pwd = document.getElementsByTagName("input")[1].value;
    if(pwd == ""){
        alert("請輸入密碼!");
        return false;
    }
    var name = document.getElementsByTagName("input")[2].value;
    if(name == ""){
        alert("請輸入姓名!");
        return false;
    }
}
</script>

後台註冊處理

<?php
$uid = $_POST["uid"];
$pwd = $_POST["pwd"];
$name = $_POST["name"];
$sex = $_POST["sex"];

$db = new MySQLi("localhost","root","","0710_info");
if(mysqli_connect_error()){
    die("串連失敗");    
}
$sql = "insert into users values(‘{$uid}‘,‘{$pwd}‘,‘{$name}‘,{$sex})";
$r = $db->query($sql);
if($r){
    echo "註冊成功!";
}else{
    echo "註冊失敗!";    
}

?>

PHP讀取mysql資料庫——以表格形式顯示資料

<body>
<table width="50%" border="1">
    <tr>
        <td>代號</td>
        <td>姓名</td>
        <td>性別</td>
        <td>生日</td>
        <td>民族</td>
    </tr>
<?php
$db = new MySQLi("localhost","root","","0710_info");
$sql = "select info.code,info.name,sex,birthday,nation.name from info join nation on info.nation=nation.code";//聯集查詢
$result = $db->query($sql);
$arr = $result->fetch_all();
//var_dump($arr);

//利用foreach迴圈遍曆所有資料
foreach($arr as $v){
    $sex = $v[2]?‘男‘:‘女‘;//利用三元運算子判斷性別
    echo "<tr>
        <td>{$v[0]}</td>
        <td>{$v[1]}</td>
        <td>{$sex}</td>
        <td>{$v[3]}</td>
        <td>{$v[4]}</td>
        </tr>";    
}
?>
</table>
</body>

利用PHP實現登入與註冊功能以及使用PHP讀取mysql資料庫——以表格形式顯示資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.