新聞中心
PHP用戶登陸頁(yè)面查詢數(shù)據(jù)庫(kù)
兩個(gè)都有問題才對(duì),你的SQL語句有錯(cuò),假設(shè)$_POST['username']='user';
成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)元氏,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
$_POST['password']='pass';你得到的語句是SELECT username FROM DB_TABLE WHERE name= user and password= pass
而正確的語句應(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è)面。
用戶名
密 碼
創(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獲得用戶名表單值
$pwd = sha1($_POST['password']);//post獲得用戶密碼單值
if ($name $pwd){//如果用戶名和密碼都不為空
$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 "用戶名或密碼錯(cuò)誤";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁(yè)面重試;
}
}else{//如果用戶名或密碼有空
echo "表單填寫不完整";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";
//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁(yè)面重試;
}
$pdo = null;
創(chuàng)建signup.html,注冊(cè)頁(yè)面
用戶名:
密 碼:
創(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ù)插入表單傳來的值的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訪問MySQL數(shù)據(jù)庫(kù)的步驟。
PHP訪問MySQL數(shù)據(jù)庫(kù):
因?yàn)檫B接數(shù)據(jù)庫(kù)需要較長(zhǎng)的時(shí)間和較大的資源開銷,所以如果在多個(gè)網(wǎng)頁(yè)中都要頻繁地訪問數(shù)據(jù)庫(kù),則可以建立與數(shù)據(jù)庫(kù)的持續(xù)連接。即調(diào)用mysql_pconnect()代替mysql_connect()。
基本步驟:
1.連接服務(wù)器:mysql_connect();
2.選擇數(shù)據(jù)庫(kù):mysql_select_db();
3.執(zhí)行SQL語句:mysql_query();
查詢:select
顯示:show
插入:insert into
更新:update
刪除:delete
4.關(guān)閉結(jié)果集:mysql_free_result($result);
5.關(guān)閉數(shù)據(jù)庫(kù):mysql_close($link);
phpstudy怎樣進(jìn)入數(shù)據(jù)庫(kù)?
phpstudy數(shù)據(jù)庫(kù)創(chuàng)建步驟:
1、點(diǎn)擊打開phpstudy軟件,然后點(diǎn)擊mySQL管理器;
2、進(jìn)入到PHPmyadmin登陸界面,默認(rèn)賬號(hào)和密碼都是root;
3、登陸進(jìn)去;
4、然后,點(diǎn)擊數(shù)據(jù)庫(kù),輸入想要的數(shù)據(jù)名稱;
5、點(diǎn)擊創(chuàng)建后,成功!
網(wǎng)站欄目:php登錄界面訪問數(shù)據(jù)庫(kù) php 訪問數(shù)據(jù)庫(kù)
分享地址:http://fisionsoft.com.cn/article/dosoieg.html