新聞中心
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ù)
當(dāng)前標(biāo)題:php登錄頁(yè)數(shù)據(jù)庫(kù) php注冊(cè)登錄連接數(shù)據(jù)庫(kù)簡(jiǎn)單代碼
本文鏈接:http://www.ef60e0e.cn/article/ddipojc.html