php登錄頁(yè)數(shù)據(jù)庫(kù) php注冊(cè)登錄連接數(shù)據(jù)庫(kù)簡(jiǎn)單代碼

php 分別寫(xiě)出用戶(hù)登錄頁(yè)和post數(shù)據(jù)傳送方式下的數(shù)據(jù)庫(kù)插入的程序段(注:用戶(hù)登錄頁(yè)為index

?php

專(zhuān)注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)白城免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

if(isset($_POST["submit"]) $_POST["submit"] == "登陸")

{

$user = $_POST["username"];

$psw = $_POST["password"];

if($user == "" || $psw == "")

{

echo "scriptalert('請(qǐng)輸入用戶(hù)名或密碼!'); history.go(-1);/script";

}

else

{

mysql_connect("localhost","root","");

mysql_select_db("vt");

mysql_query("set names 'gbk'");

$sql = "select username,password from user where username = '$_POST[username]' and password = '$_POST[password]'";

$result = mysql_query($sql);

$num = mysql_num_rows($result);

if($num)

{

$row = mysql_fetch_array($result); //將數(shù)據(jù)以索引方式儲(chǔ)存在數(shù)組中

header("location:QA.php");

echo $row[0];

}

else

{

echo "scriptalert('用戶(hù)名或密碼不正確!');history.go(-1);/script";

}

}

}

else

{

echo "scriptalert('提交未成功!'); history.go(-1);/script";

}

?

這里提供個(gè)參考

PHP用戶(hù)登陸頁(yè)面查詢(xún)數(shù)據(jù)庫(kù)

兩個(gè)都有問(wèn)題才對(duì),你的SQL語(yǔ)句有錯(cuò),假設(shè)$_POST['username']='user';

$_POST['password']='pass';你得到的語(yǔ)句是SELECT username FROM DB_TABLE WHERE name= user and password= pass

而正確的語(yǔ)句應(yīng)該是SELECT username FROM DB_TABLE WHERE name= 'user' and password= 'pass'(掉了幾個(gè)引號(hào)所以報(bào)錯(cuò))

php登錄頁(yè)面完整代碼連接數(shù)據(jù)庫(kù)

創(chuàng)建conn.php,連接數(shù)據(jù)庫(kù)。

$dns = 'mysql:host=127.0.0.1;dbname=test';

$username = 'root';

$password = 'root';

// 1.連接數(shù)據(jù)庫(kù),創(chuàng)建PDO對(duì)象

$pdo = new PDO($dns,$username,$password);

創(chuàng)建login.html,登陸頁(yè)面。

用戶(hù)名

密 碼

創(chuàng)建login.php,驗(yàn)證賬號(hào)密碼。

header("Content-Type: text/html; charset=utf8");

if(!isset($_POST["submit"])){

exit("錯(cuò)誤執(zhí)行");

}//檢測(cè)是否有submit操作

include('conn.php');//鏈接數(shù)據(jù)庫(kù)

$name = $_POST['name'];//post獲得用戶(hù)名表單值

$pwd = sha1($_POST['password']);//post獲得用戶(hù)密碼單值

if ($name $pwd){//如果用戶(hù)名和密碼都不為空

$sql = "select * from user where username = '$name' and password='$pwd'";//檢測(cè)數(shù)據(jù)庫(kù)是否有對(duì)應(yīng)的username和password的sql

$stmt = $pdo-prepare($sql);

$stmt-execute();

if($stmt-fetch(PDO::FETCH_BOUND)){//0 false 1 true

header("refresh:0;url=welcome.html");//如果成功跳轉(zhuǎn)至welcome.html頁(yè)面

exit;

}else{

echo "用戶(hù)名或密碼錯(cuò)誤";

echo "

setTimeout(function(){window.location.href='login.html';},1000);

";//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁(yè)面重試;

}

}else{//如果用戶(hù)名或密碼有空

echo "表單填寫(xiě)不完整";

echo "

setTimeout(function(){window.location.href='login.html';},1000);

";

//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁(yè)面重試;

}

$pdo = null;

創(chuàng)建signup.html,注冊(cè)頁(yè)面

用戶(hù)名:

密 碼:

創(chuàng)建signup.php

header("Content-Type: text/html; charset=utf8");

if(!isset($_POST['submit'])){

exit("錯(cuò)誤執(zhí)行");

}//判斷是否有submit操作

$name=$_POST['name'];//post獲取表單里的name

$pwd = sha1($_POST['password']);//post獲取表單里的password

include('conn.php');//鏈接數(shù)據(jù)庫(kù)

$sql="insert into user(id,username,password) values (null,'$name','$pwd')";//向數(shù)據(jù)庫(kù)插入表單傳來(lái)的值的sql

$stmt = $pdo-prepare($sql);

$stmt-execute();

$stmt-fetch(PDO::FETCH_BOUND);

if (!$stmt){

die('Error: ' . $stmt-getMessage());//如果sql執(zhí)行失敗輸出錯(cuò)誤

}else{

echo "注冊(cè)成功";//成功輸出注冊(cè)成功

}

$pdo = null;//關(guān)閉數(shù)據(jù)庫(kù)

分享題目:php登錄頁(yè)數(shù)據(jù)庫(kù) php注冊(cè)登錄連接數(shù)據(jù)庫(kù)簡(jiǎn)單代碼
分享鏈接:http://www.muchs.cn/article22/ddipojc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、標(biāo)簽優(yōu)化網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作App設(shè)計(jì)、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)